为什么我的 didBeginContact 函数检测到太多碰撞?
Why is my didBeginContact function detecting way too many collisions?
我的 didBeginContact 函数检测到两个 SKSpriteNode 之间的大约 60 次碰撞,而实际上应该只有一个。
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if firstBody.categoryBitMask == bulletCategory && secondBody.categoryBitMask == dragonCategory || firstBody.categoryBitMask == dragonCategory && secondBody.categoryBitMask == bulletCategory{
print("collision happened")
}
}
当节点开始接触时,控制台会多次打印 "collision happened"。
下面是我如何为每个 SKSpriteNode 声明物理体
dragonNode.physicsBody = SKPhysicsBody(texture: dragonNode.texture!, size: CGSizeMake(dragonNode.size.width, dragonNode.size.height))
dragonNode.physicsBody?.affectedByGravity = false
dragonNode.physicsBody?.dynamic = true
dragonNode.physicsBody?.categoryBitMask = dragonCategory
dragonNode.physicsBody?.collisionBitMask = bulletCategory
dragonNode.physicsBody?.contactTestBitMask = bulletCategory
bulletNode.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
bulletNode.physicsBody?.affectedByGravity = false
bulletNode.physicsBody?.dynamic = true
bulletNode.physicsBody?.categoryBitMask = bulletCategory
bulletNode.physicsBody?.collisionBitMask = dragonCategory
bulletNode.physicsBody?.contactTestBitMask = dragonCategory
这是我为完成游戏而需要修复的最后一件事,所以如果有人能提供帮助那就太好了!
这是因为理论上您的游戏是 运行 每秒 60 帧。所以它每 60 秒检测一次碰撞并打印警告。您还没有输入任何代码来停止检测此碰撞。你总是可以在对象上放一个标志
dragonNode.collided = true
然后检测碰撞和标志
if (firstBody.categoryBitMask == bulletCategory && secondBody.categoryBitMask == dragonCategory || firstBody.categoryBitMask == dragonCategory && secondBody.categoryBitMask == bulletCategory) && dragonNode.collided != true {
dragonNode.collided = true
print("collision happened")
}
这样碰撞只会触发一次
我的 didBeginContact 函数检测到两个 SKSpriteNode 之间的大约 60 次碰撞,而实际上应该只有一个。
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if firstBody.categoryBitMask == bulletCategory && secondBody.categoryBitMask == dragonCategory || firstBody.categoryBitMask == dragonCategory && secondBody.categoryBitMask == bulletCategory{
print("collision happened")
}
}
当节点开始接触时,控制台会多次打印 "collision happened"。
下面是我如何为每个 SKSpriteNode 声明物理体
dragonNode.physicsBody = SKPhysicsBody(texture: dragonNode.texture!, size: CGSizeMake(dragonNode.size.width, dragonNode.size.height))
dragonNode.physicsBody?.affectedByGravity = false
dragonNode.physicsBody?.dynamic = true
dragonNode.physicsBody?.categoryBitMask = dragonCategory
dragonNode.physicsBody?.collisionBitMask = bulletCategory
dragonNode.physicsBody?.contactTestBitMask = bulletCategory
bulletNode.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
bulletNode.physicsBody?.affectedByGravity = false
bulletNode.physicsBody?.dynamic = true
bulletNode.physicsBody?.categoryBitMask = bulletCategory
bulletNode.physicsBody?.collisionBitMask = dragonCategory
bulletNode.physicsBody?.contactTestBitMask = dragonCategory
这是我为完成游戏而需要修复的最后一件事,所以如果有人能提供帮助那就太好了!
这是因为理论上您的游戏是 运行 每秒 60 帧。所以它每 60 秒检测一次碰撞并打印警告。您还没有输入任何代码来停止检测此碰撞。你总是可以在对象上放一个标志
dragonNode.collided = true
然后检测碰撞和标志
if (firstBody.categoryBitMask == bulletCategory && secondBody.categoryBitMask == dragonCategory || firstBody.categoryBitMask == dragonCategory && secondBody.categoryBitMask == bulletCategory) && dragonNode.collided != true {
dragonNode.collided = true
print("collision happened")
}
这样碰撞只会触发一次