2015-02-11 106 views
0

嗨我想改進我的遊戲中兩個節點之間的碰撞邏輯。當玩家與硬幣接觸時,我想更新分數並隱藏節點。它可以正常工作,但在與隱藏節點的聯繫中分數會更新多次。我想知道是否有辦法只運行一次,以便分數更新一次。這裏是我的代碼第一次接觸後立即結束精靈套件碰撞

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

做刪除它只是隱藏節點?或從父母中刪除它? – rakeshbs 2015-02-11 17:19:16

回答

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