gpt4 book ai didi

swift - 物理多次检测碰撞

转载 作者:可可西里 更新时间:2023-11-01 01:42:22 26 4
gpt4 key购买 nike

当球与杯子碰撞时,分数应增加 1。但是目前它增加 1、3 或有时 4,这意味着多次检测到碰撞。

我想我一定是错误地检查了碰撞:

let ballCategory   : UInt32 = 0x1 << 0 
let cupCategory : UInt32 = 0x1 << 1

在我的 didMoveToView 中:

//Set physicsBody of scene to a physics body that borders the screen

self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
physicsWorld.contactDelegate = self

//Ball
ballSprite.physicsBody = SKPhysicsBody(rectangleOfSize: ballSprite.size)
ballSprite.physicsBody!.categoryBitMask = ballCategory
ballSprite.physicsBody!.contactTestBitMask = cupCategory

//Cup
cupSprite.physicsBody = SKPhysicsBody(edgeLoopFromRect: cupSprite.size)
cupSprite.physicsBody!.categoryBitMask = cupCategory
cupSprite.physicsBody!.contactTestBitMask = ballCategory

在我的 didBeginContact 中:

var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody

//Assign the two physics bodies so that the one with the lower category is always stored in firstBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {

firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {

firstBody = contact.bodyB
secondBody = contact.bodyA
}

//contact between ball and cup
if firstBody.categoryBitMask == ballCategory && secondBody.categoryBitMask == cupCategory {

score++
println("point scored")
moveBall() //Move ball back to start position
}

这在检测碰撞的意义上是有效的,但是它会发生多次,而它应该只发生一次。

最佳答案

您可以设置一个 BOOL 值来检查是否允许接触处理(一开始设置 YES)。更新分数后,您可以将该变量设置为 NO,然后再次运行 moveBall() 并在 moveBall() 方法结束时将 BOOL 值设置为 YES。我认为这样的事情应该可行。

其他方法是从父节点中删除球/或杯节点并将其设置为零(然后重新创建它们)。我也不知道 SpriteKit 有多“喜欢”这种方式(在完成物理模拟之前删除节点)。

第三种方法是让 Ball 类具有一些 BOOL 属性,指示球是否应该移动到起始位置。基于该值,您应该更新分数变量/处理接触和碰撞。因此,在更新分数后,将 BOOL 值设置为 YES,然后运行 ​​moveBall(),然后将 BOOL 值设置为 NO 等...与我描述的第一种方法类似。

关于swift - 物理多次检测碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28526694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com