gpt4 book ai didi

ios - SpriteKit 中的完成 block 从未被调用

转载 作者:行者123 更新时间:2023-11-30 12:59:43 24 4
gpt4 key购买 nike

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

    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 block 的调用。 addPlank 相同。您需要插入对 completion() 的调用。

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

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

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

关于ios - SpriteKit 中的完成 block 从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39988531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com