首次接触后立即结束精灵工具包碰撞

时间:2015-02-11 17:11:46

标签: iphone swift ios8 sprite-kit

您好我正在尝试改进我的游戏中两个节点之间碰撞的逻辑。我想更新分数并在玩家与硬币接触时隐藏节点。它工作正常,但在与隐藏节点的接触过程中,分数会多次更新。我想知道是否有办法只运行一次,以便分数更新一次。这是我的代码

    //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)
    }
}

1 个答案:

答案 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)
         }
    }
}