swiftsprite-kitskaction

Pause an SKAction in Spritekit with Swift


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


Solution

  • 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