[英]completion blocks in SpriteKit never called
我目前有一些完成块,应该在完成滑动手势后执行。 我将断点放在应该在完成块中调用的函数中,但从未触发过。 滑动手势有效,但我不确定为什么不调用完成块。 这是我的代码:
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))
}
}
}
swipeAndAddPlank
不包含对传递给它的completion
块的调用。 与addPlank
相同。 您需要插入对completion()
的调用。
您可能正在寻找SKAction.runBlock
。 就像是
let completionAction = SKAction.runBlock({ completion() })
然后将completionAction
添加到plankWood设置为运行的操作序列中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.