2016-10-11 2 views
3

Я пытаюсь вызвать функцию, когда спрайт удаляется из родителя, поэтому спрайт может реплицироваться и снова вводить сцену.
Каждый раз, когда я делаю это в своем текущем коде, он реплицируется до того, как исходный спрайт удаляется из родителя, что приводит к дублированию спрайтов.

Вот мой код до сих пор:SpriteKit: Как вызвать функцию в SKAction.sequence?

import SpriteKit 


let plankName = "woodPlank" 

class PlankScene: SKScene { 

    var plankWood : SKSpriteNode? 

    var plankArray : [SKSpriteNode] = [] 

    override func didMove(to view: SKView) { 

    plankWood = childNode(withName: plankName) as? SKSpriteNode 


    let swipeRight : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(PlankScene.swipedRight)) 

    swipeRight.direction = .right 

    view.addGestureRecognizer(swipeRight) 

    } 


    func swipedRight(sender: UISwipeGestureRecognizer) { 

    if sender.direction == .right { 

    let moveOffScreenRight = SKAction.moveTo(x: 400, duration: 0.5) 

    let nodeFinishedMoving = SKAction.removeFromParent() 

     plankWood?.run(SKAction.sequence([moveOffScreenRight, nodeFinishedMoving])) 


    } 
    //Functions To Be Call After Sequence Finishes 
    addPlank() 
    movePlanksUp() 


    } 


    func addPlank() { 
    let newPlank = plankWood?.copy() as! SKSpriteNode 
    newPlank.position = CGPoint(x: 0, y: -250) 
    plankArray.append(newPlank) 
    print(plankArray.count) 
    addChild(newPlank) 

    } 

    func movePlanksUp() { 
    for node:SKSpriteNode in plankArray { 
     node.run(SKAction.move(by: CGVector(dx: 0, dy: 250), duration: 0.10)) 
    } 
    } 


} 

ответ

1

завершение

заменить:

plankWood?.run(SKAction.sequence([moveOffScreenRight, nodeFinishedMoving])) 
    } 
    //Functions To Be Call After Sequence Finishes 
    addPlank() 
    movePlanksUp() 

с:

plankWood?.run(SKAction.sequence([moveOffScreenRight, nodeFinishedMoving]), 
    completion:{ 
     //Functions To Be Call After Sequence Finishes 
     addPlank() 
     movePlanksUp()})