您好我正在尝试改进我的游戏中两个节点之间碰撞的逻辑。我想更新分数并在玩家与硬币接触时隐藏节点。它工作正常,但在与隐藏节点的接触过程中,分数会多次更新。我想知道是否有办法只运行一次,以便分数更新一次。这是我的代码
//MARK: SKPhysicsContactDelegate methods
func didBeginContact(contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == userCategory) && (contact.bodyB.categoryBitMask == objectCategory) {
gameOver = 1
movingObjects.speed = 0
presentGameOverView()
}
if (contact.bodyA.categoryBitMask == userCategory) && (contact.bodyB.categoryBitMask == coinCategory) {
coinSound()
contact.bodyB.node?.hidden = true
score = score + 1
println(score)
}
}
答案 0 :(得分:1)
您可以在提高分数之前检查节点是否隐藏。
func didBeginContact(contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == userCategory) && (contact.bodyB.categoryBitMask == objectCategory) {
gameOver = 1
movingObjects.speed = 0
presentGameOverView()
}
if (contact.bodyA.categoryBitMask == userCategory) && (contact.bodyB.categoryBitMask == coinCategory) {
if !contact.bodyB.node?.hidden // Added line
{
coinSound()
contact.bodyB.node?.hidden = true
score = score + 1
println(score)
}
}
}
将其从父母
中删除func didBeginContact(contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == userCategory) && (contact.bodyB.categoryBitMask == objectCategory) {
gameOver = 1
movingObjects.speed = 0
presentGameOverView()
}
if (contact.bodyA.categoryBitMask == userCategory) && (contact.bodyB.categoryBitMask == coinCategory) {
if contact.bodyB.node?.parent != nil {
coinSound()
contact.bodyB.node?.removeFromParent()
score = score + 1
println(score)
}
}
}