gpt4 book ai didi

ios - SpriteKit : How to call a function within a SKAction. 序列?

转载 作者:搜寻专家 更新时间:2023-10-31 22:58:54 30 4
gpt4 key购买 nike

一旦 Sprite 从父级移除,我会尝试调用一个函数,以便 Sprite 可以复制并再次进入场景。
每次我在当前代码中执行此操作时,它都会在原始 Sprite 出现之前进行复制从父项中删除导致重复的 Sprite 。

这是我目前的代码:

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


}

最佳答案

完成

替换:

plankWood?.run(SKAction.sequence([moveOffScreenRight, nodeFinishedMoving]))
}
//Functions To Be Call After Sequence Finishes
addPlank()
movePlanksUp()

与:

    plankWood?.run(SKAction.sequence([moveOffScreenRight, nodeFinishedMoving]), 
completion:{
//Functions To Be Call After Sequence Finishes
addPlank()
movePlanksUp()} )

关于ios - SpriteKit : How to call a function within a SKAction. 序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39987539/

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