我的didBeginContact函数在两个SKSpriteNodes之间检测到大约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")
}
}
当节点接触时,控制台会多次打印“碰撞发生”。
以下是我为每个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
这是我需要修复的最后一件事,我的游戏才能完成,所以如果有人能提供帮助那就太棒了!
答案 0 :(得分:2)
这是因为理论上你的游戏以每秒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")
}
这样碰撞只会发射一次