Fayyouz
Fayyouz

Reputation: 682

Apply a sequence of different SKActions to different SKNodes

I want to create a sequence of SKActions, each of which is for a different SKSpriteNode. I want to fade my layer node, remove it from the parent, wait for 3 seconds, then start a move SKAction for my snake node. Here's some code:

func startGame() {
    layer.run(SKAction.sequence([
        SKAction.fadeAlpha(to: 0, duration: 1),
        SKAction.removeFromParent(),
        SKAction.wait(forDuration: 1),
    ]))
    //Here move the snake node
}

The problem is, if I add snake.run(SKAction.move(...)) at the place of the comment, it gets executed at the same time with layer.run(...).

Upvotes: 2

Views: 92

Answers (1)

Kane Cheshire
Kane Cheshire

Reputation: 1712

You can run a block of code or function as an action (because in Swift closures are also function types), so you can add SKAction.runBlock(moveSnake) as the last SKAction on the array of actions run in layer.

Then in func moveSnake() {} you can run your actions to move the snake.

Upvotes: 4

Related Questions