SpriteKit:如何调用SKAction.sequence中的函数?

时间:2016-10-11 22:06:24

标签: 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 个答案:

答案 0 :(得分:1)

完成

取代:

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