2015-11-20 34 views
1

試圖做一些我覺得應該很簡單,但沒有任何我已經嘗試到目前爲止的作品。我試圖阻止這個循環繼續,直到我敵人的屬性被設置爲true。在繼續循環之前等到屬性爲真

我的敵方節點在步行狀態中找出了玩家的路徑。我不想迭代到下一個敵人,直到路徑被計算出來。我的敵方節點有一個pathComplete節點,我在walk狀態期間設置爲true。

這是觸摸式執行的。

 for node:AnyObject in self.children { 


       if node is EnemyNode { 

        let enemy = node as! EnemyNode 

        enemy.destination = coordinate 
        enemy.stateMachine.enterState(WalkingState) 


       } 

      } 
+0

uhm ...'如果取消{break}'? – Arbitur

回答

0

如果我知道你想做什麼,那麼你應該使用遞歸代替循環。

首先,您需要創建一些enemyNodeArray,它將包含您需要的對象。

然後你就可以讓兩個函數是這樣的:

func actionForObjectWithIndex(index: Int, completion block: (nextIndex: Int) -> Void) { 
    guard index >= 0 && index < enemyNodeArray.count else { 
     return 
    } 

    // do what you need with object in array like enemyNodeArray[index]... 
    ... 
    // Then call completion 
    block(nextIndex: index + 1) 
} 

func makeActionWithIndex(index: Int) { 
    actionForObjectWithIndex(index, completion: {(nextIndex: Int) -> Void in 
     self.makeActionWithIndex(nextIndex) 
    }) 
} 

,並開始使用它像這樣:

if !enemyNodeArray.isEmpty { 
    makeActionWithIndex(0) 
} 

該算法將採取在陣列中的每個對象,並執行某些動作他們,並且只有在完成之前它纔會移動到下一個項目。

相關問題