【问题标题】:completion blocks in SpriteKit never calledSpriteKit 中的完成块从未调用过
【发布时间】:2016-10-11 23:57:08
【问题描述】:

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

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


}

【问题讨论】:

    标签: ios iphone swift sprite-kit skscene


    【解决方案1】:

    swipeAndAddPlank 不包含对传递给它的completion 块的调用。 addPlank 也一样。您需要插入对completion() 的呼叫。

    您可能正在寻找SKAction.runBlock。类似的东西

    let completionAction = SKAction.runBlock({ completion() })
    

    然后将completionAction 添加到 plankWood 设置为运行的操作序列中。

    【讨论】:

    • 这就是我要找的!但由于某些奇怪的原因,我收到一个错误“无法将 '()' 类型的值转换为预期的参数类型 '() -> Void'”
    • 这是我用的:let completionAction = SKAction.runBlock(addPlank{completion!()})
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    相关资源
    最近更新 更多