2015-11-16 147 views
-3

我正在開發一款遊戲,它包含在場景中移動的幾個箭頭。我已經設定了一些碰撞,我需要移除與障礙物相遇的箭頭。我設置的非常簡單而且功能強大,但只有一個箭頭,因爲它是數組的元素[0],顯然如果另一個(不是[0])觸及屏障,它將不會從父級移除。儘管需要爲每個數字手動賦值數組元素,有什麼辦法可以將碰撞委託給所有元素嗎?選擇數組中的任何元素

// 3. react to the contact between the two nodes 
     if firstBody.categoryBitMask == barrier?.physicsBody?.categoryBitMask && 
      secondBody.categoryBitMask == arrows[0].physicsBody?.categoryBitMask { 
       // Player & barrier 

       gameOver(false) 
     } else if firstBody.categoryBitMask == goal?.physicsBody?.categoryBitMask && secondBody.categoryBitMask == arrows[0].physicsBody?.categoryBitMask { 
      // Player & Goal 

      arrows[0].removeFromParent() 
//   gameOver(true) 

謝謝!

:)

回答

1

然後你初始化arrowgoal你應該給他們categoryBitMask小號

arrow.physicsBody?.categoryBitMask = 0x1 << 0 
goal.physicsBody?.categoryBitMask = 0x1 << 1 
barrie.physicsBody?.categoryBitMask = 0x1 << 2 

此外,我建議你創建Constants.swift文件和存儲您的常量存在。 Constants.swift

let kCategoryArrow: UInt32 = 0x1 << 0 
let kCategoryGoal: UInt32 = 0x1 << 1 
let kCategoryBarrie: UInt32 = 0x1 << 2 

然後你就可以給categoryBitMask就像這樣(那麼你對它們進行初始化)

arrow.physicsBody?.categoryBitMask = kCategoryArrow 
goal.physicsBody?.categoryBitMask = kCategoryGoal 
barrier.physicsBody?.categoryBitMask = kCategoryBarrier 

而最後一件事接觸檢查:

/* Arrow vs Barrier */ 

    if bodyA.physicsBody?.categoryBitMask == kCategoryArrow && bodyB.physicsBody?.categoryBitMask == kCategoryBarrier { 
     gameOver(false) 
    } else if bodyA.physicsBody?.categoryBitMask == kCategoryBarrier && bodyB.physicsBody?.categoryBitMask == kCategoryBullet { 
     gameOver(false) 
    } 

    /* Arrow vs Goal */ 

    if bodyA.physicsBody?.categoryBitMask == kCategoryArrow && bodyB.physicsBody?.categoryBitMask == kCategoryGoal { 
     bodyA.node.removeFromParent() 
    } else if bodyA.physicsBody?.categoryBitMask == kCategoryGoal && bodyB.physicsBody?.categoryBitMask == kCategoryBullet { 
     bodyB.node.removeFromParent() 
    } 
+0

我所有的初始化'categoryBitMask'。問題是每個箭頭在碰撞時應該消失,但它們位於數組內。我需要一個解決方案,只能刪除聯繫人,而不是全部。 –

+0

@AlessandroNardinelli這個解決方案是刪除你的箭頭與你的goall聯繫。你甚至不需要一個數組。 – Darvydas