11

I have the following code to move an SKSpriteNode.

let moveDown = SKAction.moveBy(CGVectorMake(0, -120), duration: 1)
let moveUp = SKAction.moveBy(CGVectorMake(0, +120), duration: 1)
let moveSequence = SKAction.sequence([moveDown, moveUp])
square.runAction(SKAction.repeatActionForever(moveSequence))

This moves the SKSpriteNode up and down forever. Is there a way that I could pause this SKAction? So that the SKSpriteNode will freeze in its current position, and then later when I decide, continue its movement?

I only want to pause the movement of this SKSpriteNode. I do not want to pause the SKScene. Just the movement of this 1 SKSpriteNode

Cristik
  • 30,989
  • 25
  • 91
  • 127

3 Answers3

11

You should run an action with key:

 square.runAction(SKAction.repeatActionForever(moveSequence), withKey:"moving")

Then, use action's speed property to pause it:

if let action = square.actionForKey("moving") {

        action.speed = 0       
}

or to unpause it:

action.speed = 1
Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • Thanks whirlwind! Works perfectly. If I have a group of SKSpriteNodes, lets say 5 of them. What is the best way to use action's speed property to stop them all? I tried using the key "moving" on all of them but it only stops one of the SKSpriteNodes :) –  Jan 04 '16 at 14:52
  • @Questions First, name all square nodes (eg. square.name = "square") , then run [enumerateChildNodesWithName](http://stackoverflow.com/q/24213436/3402095) method to access every square by it's name, and change its action associated with key "moving". – Whirlwind Jan 04 '16 at 15:04
11

An alternative to @Whirlwind 's answer, in case you have a bunch of actions that need to be paused that are not in a group and not just the movement, is to just pause the node itself. All SKNodes have a paused property associated with it.

Ex. square.paused = true

Update: This property is now isPaused. An example with this change would resemble below:

square.isPaused = true
David
  • 87
  • 12
Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44
0

SKNode.paused has been renamed to SKNode.isPaused.

You should be able to do the following:

Pause animation:

square.isPaused = true

Restart animation:

square.isPaused = false
Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
darthtye
  • 19
  • 7