Pause SpriteKit game with Physics SKSpriteNode child
Asked Answered
K

2

7

I have a SKSpriteNode with a dynamic physics body on a SKNode *_fgLayer.

Now, when I pause or unpause the game I want that SKSpriteNode to remain in place and not rotate or fall down. If I simply use _fgLayer.paused = YES; the SKSpriteNode does not stay in place but rather rotates and falls down. If I set physicsBody.dynamic = NO; when paused and physicsBody.dynamic = YES; when unpaused it works.

However, after a few tries I always get the following crash:

"Assertion failed: (typeA == b2_dynamicBody || typeB == b2_dynamicBody), function SolveTOI, file /SourceCache/PhysicsKit/PhysicsKit-4.6/PhysicsKit/Box2D/Dynamics/b2World.cpp, line 670."

So, I guess setting a physicsBody.dynamic = NO; and back does not work.

Does anyone have an idea of
how to pause dynamic physics bodies to remain in place when the game is paused?

Khaddar answered 9/3, 2014 at 12:11 Comment(4)
I think you should use sprite.speed = 0.0 here.Moriahmoriarty
Or try setting scene.physicsWorld.speed = 0.0 ( developer.apple.com/Library/ios/documentation/SpriteKit/… )Moriahmoriarty
UPDATE: A workaround is to make all the aspects that affect the node from the phsyicsBody to remain stable. I used the following code to pause and something similar for the unpause: _fish.physicsBody.affectedByGravity = NO; _fish.physicsBody.allowsRotation = NO; _fish.physicsBody.velocity = CGVectorMake(0, 0); _fish.physicsBody.angularVelocity = 0;Hygro
scene.physicsWorld.speed = 0.0 did the job well! Thanks!Hygro
M
23

Try setting your physicsWorld.speed to 0.0:

scene.physicsWorld.speed = 0.0

Apple SKPhysicsWorld Ref.

Moriahmoriarty answered 9/3, 2014 at 12:34 Comment(2)
How do you resume later? What speed to you reset to when you want to resume?Contrastive
My bad, I just had a read of the apple's Documentation. One can set scene.physicsWorld.speed = 1.0 to resume. 1 indicates the normal speed, 2 will be twice as fast as the normal speed.Contrastive
N
3

Setting scene.view.paused = YES works for me - specially when there are actions running on the child node.

self.scene.view setPaused:YES

Neon answered 14/12, 2015 at 14:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.