简体   繁体   中英

SpriteKit: How to call a function within a SKAction.sequence?

I'm try to call a function once a sprite is removed from a parent so the sprite can replicate and enter the scene again.
Every time I do it in my current code it replicates before the original sprite is removed from parent resulting in duplicate sprites.

Here is my code so far:

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


}

completion

replace:

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

with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM