碰撞一次,然后不要再这样做了



如何让我的SKnode只碰撞一次?我有一个粒子系统,当它们碰撞时会出现,但如果它们再次碰撞,就会出现一堆。我不想这样。只想要一个,就是这样。我将如何消除这种效果?

if bodyA.categoryBitMask == 1 && bodyB.categoryBitMask == 3 || bodyA.categoryBitMask == 3 && bodyB.categoryBitMask == 1 {
print("END GAME")

if let dieexplostionNode = SKEmitterNode(fileNamed: "Explosion.sks"){
dieexplostionNode.targetNode = self
dieexplostionNode.position = player.position
// add 3 actions
let wait = SKAction.wait(forDuration: 1.1)
let addExplosion = SKAction.run {
self.player.addChild(dieexplostionNode)
self.player.alpha = 0
}
let removePlayer = SKAction.run {
self.player.removeFromParent()
self.player.removeAllActions()
dieexplostionNode.removeFromParent()
}
// put them in a sequence.
let seq = SKAction.sequence([addExplosion,wait,removePlayer])
// run the sequence
self.run(seq)
}

您的问题是可能会发生多个联系人,因此您需要在代码中进行检查,以告诉您的系统您已经评估了一个联系人,并且不想进行任何其他评估。 我的首选方法是在类别位掩码标志上保留位 31,如果设置了,则不要评估。

guard bodyA.categoryBitMask < 1 << 31 && bodyB.categoryBitMask < 1 << 31 else {return}
if bodyA.categoryBitMask == 1 && bodyB.categoryBitMask == 3 || bodyA.categoryBitMask == 3 && bodyB.categoryBitMask == 1 {
print("END GAME")
player.categoryBitMask += 1<<31
if let dieexplostionNode = SKEmitterNode(fileNamed: "Explosion.sks"){
dieexplostionNode.targetNode = self
dieexplostionNode.position = player.position
// add 3 actions
let wait = SKAction.wait(forDuration: 1.1)
let addExplosion = SKAction.run {
self.player.addChild(dieexplostionNode)
self.player.alpha = 0
}
let removePlayer = SKAction.run {
self.player.removeFromParent()
self.player.removeAllActions()
dieexplostionNode.removeFromParent()
}
// put them in a sequence.
let seq = SKAction.sequence([addExplosion,wait,removePlayer])
// run the sequence
self.run(seq)
}

相关内容

最新更新