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

Sai*_*ait 4 sprite-kit swift

以下是我想要做的事情的说明:

在此输入图像描述

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

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

    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

我还发现SKAction.sequence要按顺序播放动作,但是,我只能将它用于对同一对象的动作.不在我的例子中的不同对象中.

Ale*_*ano 5

如果要按顺序(而不是并行)移动两个矩形,可以使用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)
        })
    }
}
Run Code Online (Sandbox Code Playgroud)