SpriteKit中的完成块从未调用过

时间:2016-10-11 23:57:09

标签: ios iphone swift sprite-kit skscene

我目前有一些完成块,应该在滑动手势完成后执行。我将断点放在应该在完成块中调用的函数内,但它们永远不会被触发。滑动手势有效,我不确定为什么没有调用完成块。这是我的代码:

    import SpriteKit


let plankName = "woodPlank"

class PlankScene: SKScene {

  var plankWood : SKSpriteNode?

  var plankArray : [SKSpriteNode] = []





  override func didMove(to view: SKView) {

    enumerateChildNodes(withName: plankName) {
      node, stop in
      self.plankWood = node 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 {

        //The functions in this completion block are never called
      swipeAndAddPlank(completion: {
        self.addPlank(completion: {
        self.movePlanksUp()
        })

      })
    }
  }


  func swipeAndAddPlank(completion: (()->Void)?) {

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

    let nodeFinishedMoving = SKAction.removeFromParent()

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


  }


  //This function never called
  func addPlank(completion: (()->Void)?) {
    let newPlank = plankWood?.copy() as! SKSpriteNode
    newPlank.position = CGPoint(x: 0, y: -259)
    plankArray.append(newPlank)
    print(plankArray.count)
    addChild(newPlank)


  }

    //This function never called
  func movePlanksUp() {
    for node:SKSpriteNode in plankArray {
      node.run(SKAction.move(by: CGVector(dx: 0, dy: 50), duration: 0.10))
    }
  }


}

1 个答案:

答案 0 :(得分:0)

swipeAndAddPlank不包含对传递给它的completion块的调用。 addPlank也是如此。您需要拨打completion()

您可能正在寻找SKAction.runBlock。像

这样的东西
let completionAction = SKAction.runBlock({ completion() })

然后将completionAction添加到行动序列中,将设置为运行。