comatose
comatose

Reputation: 1972

Spritekit: Pausing parent node does not pause child node

I am creating my first game in SpriteKit. I am trying to pause my GameScene and then run some animations via SpriteKit. The issue was that if I pause the scene using self.view?.isPaused = true everything stops. After searching around I found that the way to do this is to add another node to the scene and make everything else that you want to pause, child nodes of that node. I am trying to do that but haven't been successful. This is the code that I have

var worldNode:SKNode?
override func didMove(to view: SKView) {
    worldNode = SKNode()
    self.addChild(self.worldNode!)

    let spriteNode:SKSpriteNode = SKSpriteNode(imageNamed: "red_ball")
    spriteNode.size = CGSize(width: 40, height: 40)
    spriteNode.physicsBody = SKPhysicsBody(circleOfRadius: 15)
    spriteNode.physicsBody?.isDynamic = true

    self.worldNode?.addChild(spriteNode)
    spriteNode.physicsBody?.applyImpulse(CGVector(dx: 10, dy: 10))


    self.worldNode?.isPaused = true
    //self.view?.isPaused = true
}

when I run this I see the ball moving on the screen which should not happen according to my understanding, since I am pausing the worldNode and the spriteNode is child node of that. If I use self.view?.isPaused = true to pause the scene, then the ball is stationary as expected. Am I missing something ? how can I add nodes to the worldNode and then just pause only those nodes by pausing worldNode. Thanks !

Upvotes: 1

Views: 569

Answers (1)

Whirlwind
Whirlwind

Reputation: 13675

Well one way to pause nodes moved by impulses and forces is certainly to pause the scene:

self.isPaused = true

but I would say that you want to have other nodes unpaused (eg. HUD elements) so you added your game elements into world node.

So one way to pause physics along with pausing the node is to pause the complete physics world, like this:

self.physicsWorld.speed = 0

Upvotes: 0

Related Questions