2017-03-06 159 views
2

在我的場景包遊戲中,節點隨機添加到場景中。這些節點沿一個方向行進,直到它們超過所需的最終位置,然後它們將被移除。他們可以向上,向下,向左和向右行進。 (我使用node.runAction(SCNAction.repeatActionForever(SCNAction.moveBy(...刪除節點導致保留週期

下面是我用來檢測節點何時超過它的結束位置,以便它們可以被刪除。

我遇到的問題是,雖然這個工程,由於某種原因,它造成了保留週期,SCNActionMoveSCNActionRepeat

我發現避免這種情況的唯一方法是在遊戲結束後立即在for循環中刪除所有節點,但這並不理想,因爲遊戲可以長時間播放。

謝謝!

func renderer(renderer: SCNSceneRenderer, updateAtTime time: NSTimeInterval) { 

    // If any nodes have been spawned 
    if spawnedNodeArray.count > 0 { 

     // Get the first spawned node 
     let node = rootNode.childNodeWithName(spawnedNodeArray[0].nodeName, recursively: true)! 

     // If node is moving RIGHT, check if node has reached end position 
     if spawnedNodeArray[0].Direction == "RIGHT" { 
      // If node has reached past end position... 
      if node.position.x >= Float(spawnedNodeArray[0].EndXPos) { 
       node.removeAllActions() 
       node.removeFromParentNode() 
       spawnedNodeArray.removeAtIndex(0) 
      } 
     } 

     // If node is moving LEFT, check if node has reached end position 
     else if spawnedNodeArray[0].Direction == "LEFT" { 
      // If node has reached past end position... 
      if node.position.x <= Float(spawnedNodeArray[0].EndXPos) { 
       node.removeAllActions() 
       node.removeFromParentNode() 
       spawnedNodeArray.removeAtIndex(0) 
      } 
     } 

     // If node is moving DOWN, check if node has reached end position 
     else if spawnedNodeArray[0].Direction == "DOWN" { 
      // If node has reached past end position... 
      if node.position.z >= Float(spawnedNodeArray[0].EndZPos) { 
       node.removeAllActions() 
       node.removeFromParentNode() 
       spawnedNodeArray.removeAtIndex(0) 
      } 
     } 

     // If node is moving UP, check if node has reached end position 
     else if spawnedNodeArray[0].Direction == "UP" { 
      // If node has reached past end position... 
      if node.position.z <= Float(spawnedNodeArray[0].EndZPos) { 
       node.removeAllActions() 
       node.removeFromParentNode() 
       spawnedNodeArray.removeAtIndex(0) 
      } 
     } 
    } 
} 

SCNActionRepeatSCNActionMove

回答

0

我已經想出瞭如何停止保留週期。

我不得不爲行動提供一個關鍵,然後取代所有行動,我只是刪除行動的關鍵。

例子:

node.runAction(..., forKey: "moveAction") 

node.removeActionForKey("moveAction") 
1

沒有直接回答你問的問題,但...

如何保持可重複使用的節點池?在不再需要刪除節點時,將其hidden屬性設置爲true並將其添加到重用隊列。不要總是創建一個新節點,首先嚐試將現有節點出列。

+0

偉大的建議,我會試試這個。謝謝! – P3rry