drewvy22
drewvy22

Reputation: 65

Pausing SKSpriteNode (Swift, SpriteKite)

I need to pause the balls for my game. I want them to stop in place but they are moving by impulse and if I make them non-dynamic to stop them, the impulse goes away. I'm trying to pause them and un-pause them, and they still keep going in the same direction. I tried

      ball.paused = true

but that didn't work. Anyone know?

Upvotes: 0

Views: 1047

Answers (3)

Aziz Akgul
Aziz Akgul

Reputation: 39

Here is how I solved it in my game:

var ballVelocity  = CGVector()

func pauseAction(ball: SKSpriteNode){

    if ball.dynamic {
        //Ball is moving. Save the current velocity of ball.
        ballVelocity = ball.velocity
        //Stop the ball
        ball.physicsBody.dynamic = false

    }else{
        //Ball is paused.
        //Resume ball movement.
        ball.physicsBody.dynamic = true
        //Add the saved velocity.
        ball.velocity = ballVelocity
    }  
}

Upvotes: 1

drewvy22
drewvy22

Reputation: 65

Instead of freezing the nodes, I froze the scene with scene?.physicsWorld.speed = 0 because that freezes all of the nodes and not my time or score integers which is exactly what I needed.

Upvotes: 3

sangony
sangony

Reputation: 11696

You can use ball.physicsBody.velocity = CGVectorMake(0, 0); but keep in mind that if the ball is affected by gravity, it will still drop.

Upvotes: 0

Related Questions