2014-12-04 120 views
0

我遇到了我設置的SKAction序列的問題。Swift中Sprite Kit SKAction的問題

該序列的目標是獲取9個精靈節點的數組,並一次顯示一個節點。然後,該序列將顯示當前節點右側或左側的節點,具體取決於按下哪個按鈕(右按鈕或左按鈕)。

我在這裏遇到一些有趣的事情。它看起來像現在一樣工作,但是如果我多次快速按下按鈕左側或右側按鈕,似乎無法跟上並且進程失敗。我沒有收到錯誤,精靈節點只是不顯示或不正確顯示。

我附上我的代碼如下。因爲它在慢慢循環時正在工作,所以我認爲這是一個時間問題,也許我需要添加一個完成塊。我試過這樣做,但沒有成功。

我的問題是,是否有任何明顯突出顯示錯誤在這裏,你認爲完成塊可能可以解決這個問題?

func pressedButtonRight(){ 
    if currentDisplayedWorld < 8 { 
     var currentWorld:SKSpriteNode = worldArray[currentDisplayedWorld] as SKSpriteNode 
     var nextWorld:SKSpriteNode = worldArray[currentDisplayedWorld + 1] as SKSpriteNode 
     nextWorld.position = CGPointMake(self.frame.size.width, 50) 
     self.addChild(nextWorld) 

     let move = SKAction.moveByX(-self.frame.size.width, y: 0, 
      duration: 1.0, delay: 0, 
      usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5) 

     //currentWorld.runAction(move) 
     nextWorld.runAction(move) 
     currentWorld.removeFromParent() 

     currentDisplayedWorld++ 
    }else if currentDisplayedWorld == 8 { 

    } 
} 

func pressedButtonLeft(){ 
    if currentDisplayedWorld > 0 { 
     var currentWorld:SKSpriteNode = worldArray[currentDisplayedWorld] as SKSpriteNode 
     var previousWorld:SKSpriteNode = worldArray[currentDisplayedWorld - 1] as SKSpriteNode 
     previousWorld.position = CGPointMake(-self.frame.size.width, 50) 

     self.addChild(previousWorld) 

     let moveBack = SKAction.moveByX(self.frame.size.width, y: 0, 
      duration: 1.0, delay: 0, 
      usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5) 

     //currentWorld.runAction(moveBack) 
     previousWorld.runAction(moveBack) 
     currentWorld.removeFromParent() 

     currentDisplayedWorld-- 
    }else if currentDisplayedWorld == 0 { 

    } 
} 
+0

有一個叫做moving的類屬性布爾值。如果移動是錯誤的,則只移動節點。在將節點集移動到true之前。使用完成處理程序在節點上運行移動操作,該處理程序將再次移動爲false。 – Okapi 2014-12-05 07:14:12

+0

因此,代碼實際上並不會停止任何已經運行的操作。如果您快速按下按鈕,則可能會同時執行多個移動操作。該行爲是未定義的。使用鍵運行動作,以便運行動作可以自動停止已經運行的其他具有相同鍵的動作。 – LearnCocos2D 2014-12-05 09:24:47

+0

好吧,這是有道理的。我今天會試試這個,並告訴你它是否成功。謝謝。 – 2014-12-05 14:13:40

回答

0

解決完美。我在下面發佈了我的最終代碼,以供所有人查看。再次感謝您的幫助。

func pressedButtonRight(){ 
    if currentDisplayedWorld < 8 { 

     if self.moving == false { 
      self.moving = true 

      var currentWorld:SKSpriteNode = self.worldArray[currentDisplayedWorld] as SKSpriteNode 
      var nextWorld:SKSpriteNode = self.worldArray[currentDisplayedWorld + 1] as SKSpriteNode 
      nextWorld.position = CGPointMake(self.frame.size.width, 50) 

      self.addChild(nextWorld) 

      let move = SKAction.moveByX(-self.frame.size.width, y: 0, duration: 1.0, delay: 0,usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5) 

      //currentWorld.runAction(move) 
      nextWorld.runAction(move, completion: { 
       self.moving = false 
      }) 
      currentWorld.removeFromParent() 
      currentDisplayedWorld++ 
     } 

    }else if currentDisplayedWorld == 8 { 

    } 
} 

func pressedButtonLeft(){ 
    if currentDisplayedWorld > 0 { 

     if self.moving == false { 
      self.moving = true 

      var currentWorld:SKSpriteNode = self.worldArray[currentDisplayedWorld] as SKSpriteNode 
      var previousWorld:SKSpriteNode = self.worldArray[currentDisplayedWorld - 1] as SKSpriteNode 
      previousWorld.position = CGPointMake(-self.frame.size.width, 50) 

      self.addChild(previousWorld) 

      let moveBack = SKAction.moveByX(self.frame.size.width, y: 0, duration: 1.0, delay: 0, 
       usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5) 

      //currentWorld.runAction(moveBack) 
      previousWorld.runAction(moveBack, completion: { 
        self.moving = false 
      }) 
      currentWorld.removeFromParent() 
      currentDisplayedWorld-- 
     } 

    }else if currentDisplayedWorld == 0 { 

    } 
}