2016-05-30 25 views
1

我嘗試管理我的每個精靈的幾個動畫。我遇到的問題是當我選擇一個精靈,它的動畫正確,但是當我選擇另一個精靈時,第一個精靈的動畫停止,動畫從第二個精靈開始。但我希望動畫能夠繼續使用手指觸摸的evry sprite用戶。下面是3個功能與SWIFT代碼:SpriteKit Swift動畫只能在一個精靈一次 - iOS

override func didMoveToView(view: SKView) { 

    let urlStr = NSBundle.mainBundle().pathForResource("Video_Socle", ofType: "mov") 
    let url = NSURL(fileURLWithPath: urlStr!) 

    player = AVPlayer(URL: url) 
    NSNotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayToEndTimeNotification, object: player!.currentItem, queue: nil) 
    { notification in 
     let t1 = CMTimeMake(5, 100); 
     self.player!.seekToTime(t1) 
     self.player!.play() 
    } 


    videoNode = SKVideoNode(AVPlayer: player!) 
    videoNode!.position = CGPointMake(frame.size.width/2, frame.size.height/2) 
    videoNode!.size = CGSize(width: 2048, height: 1536) 
    videoNode!.zPosition = 0 


    background.addChild(videoNode!) 
    videoNode!.play() 



    let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(GameScene.handlePanFrom(_:))) 
    self.view!.addGestureRecognizer(gestureRecognizer) 
} 


func handlePanFrom(recognizer : UIPanGestureRecognizer) { 
    if recognizer.state == .Began { 
     var touchLocation = recognizer.locationInView(recognizer.view) 
     touchLocation = self.convertPointFromView(touchLocation) 

     self.selectNodeForTouch(touchLocation) 
    } else if recognizer.state == .Changed { 
     var translation = recognizer.translationInView(recognizer.view!) 
     translation = CGPoint(x: translation.x, y: -translation.y) 

     self.panForTranslation(translation) 

     recognizer.setTranslation(CGPointZero, inView: recognizer.view) 
    } else if recognizer.state == .Ended { 

    } 
} 

func degToRad(degree: Double) -> CGFloat { 
    return CGFloat(degree/180.0 * M_PI) 
} 

func selectNodeForTouch(touchLocation : CGPoint) { 
    // 1 
    let touchedNode = self.nodeAtPoint(touchLocation) 

    if touchedNode is SKPuzzle { 
     // 2 
     if !selectedNode.isEqual(touchedNode) { 
      selectedNode.removeAllActions() 
      selectedNode.runAction(SKAction.rotateToAngle(0.0, duration: 0.1)) 

      selectedNode = touchedNode as! SKPuzzle 

      // 3 
      if touchedNode.name! == kpuzzleNodeName { 
       let sequence = SKAction.sequence([SKAction.rotateByAngle(degToRad(-4.0), duration: 0.1), 
        SKAction.rotateByAngle(0.0, duration: 0.1), 
        SKAction.rotateByAngle(degToRad(4.0), duration: 0.1)]) 
       selectedNode.runAction(SKAction.repeatActionForever(sequence)) 
      } 
     } 
    } 
} 



func panForTranslation(translation : CGPoint) { 
    let position = selectedNode.position 

    if selectedNode.name! == kpuzzleNodeName { 
     selectedNode.position = CGPoint(x: position.x + translation.x * 2, y: position.y + translation.y * 2) 

     print (selectedNode.name) 
     print (selectedNode.name2) 
     if selectedNode.name2 == "0" { 

      Anim_Puzzle13(selectedNode) 
     } 

     print (selectedNode.name2) 
     if selectedNode.name2 == "1" { 
      Anim_Puzzle19(selectedNode) 
     } 

     print (selectedNode.name2) 
     if selectedNode.name2 == "2" { 
      Anim_Puzzle30(selectedNode) 
     } 

     print (selectedNode.name2) 
     if selectedNode.name2 == "3" { 
      Anim_Puzzle11(selectedNode) 
     } 

     print (selectedNode.name2) 
     if selectedNode.name2 == "4" { 
      Anim_Puzzle29(selectedNode) 
     } 

     print (selectedNode.name2) 
     if selectedNode.name2 == "5" { 
      Anim_Puzzle35(selectedNode) 
     } 

     } 

} 




func Anim_Puzzle13 (Node13 : SKPuzzle) { 

    let puzzle13 = SKAction.animateWithTextures(sheet_puzzle13.Puzzle13_(), timePerFrame: 0.033) 
    NPuzzle13 = Node13 
    NPuzzle13.runAction(SKAction.repeatActionForever(puzzle13)) 
    NPuzzle13.position = CGPoint(x: 500, y: 400) 
    NPuzzle13.zPosition = 1 

} 

func Anim_Puzzle19 (Node19 : SKPuzzle) { 


    let puzzle19 = SKAction.animateWithTextures(sheet_puzzle19.Puzzle19_(), timePerFrame: 0.033) 
    NPuzzle19 = Node19 
    NPuzzle19.runAction(SKAction.repeatActionForever(puzzle19)) 
    NPuzzle19.position = CGPoint(x: 600, y: 500) 
    NPuzzle19.zPosition = 1 

} 

func Anim_Puzzle30 (Node30 : SKPuzzle) { 


    let puzzle30 = SKAction.animateWithTextures(sheet_puzzle30.Puzzle30_(), timePerFrame: 0.033) 
    NPuzzle30 = Node30 
    NPuzzle30.runAction(SKAction.repeatActionForever(puzzle30)) 
    NPuzzle30.position = CGPoint(x: 700, y: 600) 
    NPuzzle30.zPosition = 1 

} 

func Anim_Puzzle11 (Node11 : SKPuzzle) { 


    let puzzle11 = SKAction.animateWithTextures(sheet_puzzle11.Puzzle11_(), timePerFrame: 0.033) 
    NPuzzle11 = Node11 
    NPuzzle11.runAction(SKAction.repeatActionForever(puzzle11)) 
    NPuzzle11.position = CGPoint(x: 800, y: 700) 
    NPuzzle11.zPosition = 1 

} 

func Anim_Puzzle29 (Node29 : SKPuzzle) { 


    let puzzle29 = SKAction.animateWithTextures(sheet_puzzle29.Puzzle29_(), timePerFrame: 0.033) 
    NPuzzle29 = Node29 
    NPuzzle29.runAction(SKAction.repeatActionForever(puzzle29)) 
    NPuzzle29.position = CGPoint(x: 900, y: 800) 
    NPuzzle29.zPosition = 1 

} 

func Anim_Puzzle35 (Node35 : SKPuzzle) { 


    let puzzle35 = SKAction.animateWithTextures(sheet_puzzle35.Puzzle35_(), timePerFrame: 0.033) 
    NPuzzle35 = Node35 
    NPuzzle35.runAction(SKAction.repeatActionForever(puzzle35)) 
    NPuzzle35.position = CGPoint(x: 1000, y: 900) 
    NPuzzle35.zPosition = 1 

} 

}

如何管理動畫繼續上觸摸埃夫裏的精靈,而不是每個我接觸的另一個精靈時間停下來?我可以再次選擇任何精靈再次進行動畫,但只有一個的時間,從來沒有更多...

感謝您的幫助,

回答

0

你的代碼是正確的。如果您使用此代理方法,或者使用UIPanGestureRecognizer,則可能是您的touchesBegan問題,可能是您觸發了一些方法來刪除您的節點(node.removeFromParent()或類似node.removeAllActions())。 就像您在屏幕上點擊一樣,repeatForEver立即停止,因爲父節點刪除了節點,或者所有操作都立即停止。

如果你想停止某些動作最好是分給runAction特定鍵:

NPuzzle30.removeActionForKey("NPuzzle30repeatForEver") 

編輯

NPuzzle30.runAction(SKAction.repeatActionForever(puzzle30),withKey: "NPuzzle30repeatForEver") 

,然後,用停止(後您的最後的代碼更新)

if touchedNode is SKPuzzle { 
     // 2 
     if !selectedNode.isEqual(touchedNode) { 
      selectedNode.removeAllActions() 
      ... 
      // here you remove all actions about your selectedNode, also repeatActionForever.. 
    } 
} 
+0

感謝您的幫助。只有當我在屏幕上選擇另一個精靈時,動畫纔會停止。但我可以再次啓動它,如果我再次選擇精靈,但我不能有超過一個精靈正在移動......這是我的代碼更大的一部分: – PGibouin

+0

我認爲你必須在你的代碼中搜索關於「removeAllActions ()「並開始從中進行調試。 ;) –

+0

亞歷山德羅,非常感謝!有用 !!我是iOS上的Swift初學者,非常樂意接受更多實驗用戶的幫助。 – PGibouin