SpriteKit: как вызвать функцию в SKAction.sequence?

#ios #swift #sprite-kit #skspritenode

#iOS #swift #sprite-kit #skspritenode

Вопрос:

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

Вот мой код на данный момент:

    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()} )