gpt4 book ai didi

swift - 在不同节点上按顺序(非异步)执行操作

转载 作者:搜寻专家 更新时间:2023-10-31 08:13:05 26 4
gpt4 key购买 nike

这是我正在尝试做的一个例子:

enter image description here

到目前为止,这是我的代码:

import SpriteKit

class GameScene: SKScene {
let mySquare1 = SKShapeNode(rectOfSize:CGSize(width: 50, height: 50))
let mySquare2 = SKShapeNode(rectOfSize:CGSize(width: 50, height: 50))

override func didMoveToView(view: SKView) {
mySquare1.position = CGPoint(x: 100, y:100)
mySquare2.position = CGPoint(x: 300, y:100)

mySquare1.fillColor = SKColor.blueColor()
mySquare2.fillColor = SKColor.blueColor()

self.addChild(mySquare1)
self.addChild(mySquare2)

let moveAction1 = SKAction.moveTo(CGPoint(x:250, y:100), duration: 1)
mySquare1.runAction(moveAction1)

let moveAction2 = SKAction.moveTo(CGPoint(x:300, y:350), duration: 1)
mySquare2.runAction(moveAction2)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

}

override func update(currentTime: CFTimeInterval) {

}
}

我的问题是,我试图同步(而不是异步)移动矩形。也就是说,我希望我的第一个矩形开始移动,完成移动,停止。然后,开始我的第二个矩形移动,完成它的移动并停止。

目前发生的情况是,当我运行我的程序时,它们同时开始移动。

我还找到了 SKAction.sequence 用于按顺序播放的 Action ,但是,我只能将其用于同一对象上的 Action 。不像在我的示例中那样在不同的对象中。

最佳答案

如果您想顺序移动两个矩形(不是平行移动),您可以像这样使用第一个操作的 completion 属性(apple docs):

import SpriteKit
class GameScene: SKScene {
let mySquare1 = SKShapeNode(rectOfSize:CGSize(width: 50, height: 50))
let mySquare2 = SKShapeNode(rectOfSize:CGSize(width: 50, height: 50))
override func didMoveToView(view: SKView) {
mySquare1.position = CGPoint(x: 100, y:100)
mySquare2.position = CGPoint(x: 300, y:100)
mySquare1.fillColor = SKColor.blueColor()
mySquare2.fillColor = SKColor.blueColor()
self.addChild(mySquare1)
self.addChild(mySquare2)
let move1 = SKAction.moveToX(250, duration: 1.0)
let move2 = SKAction.moveToY(250, duration: 1.0)
self.mySquare1.runAction(move1,completion: {
self.mySquare2.runAction(move2)
})
}
}

关于swift - 在不同节点上按顺序(非异步)执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39196137/

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