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?

Amani Elsaed
  • 1,558
  • 2
  • 21
  • 36
μ4ρκ05
  • 585
  • 2
  • 5
  • 16
  • I think you should use sprite.speed = 0.0 here. – AndrewShmig Mar 09 '14 at 12:20
  • Or try setting scene.physicsWorld.speed = 0.0 ( https://developer.apple.com/Library/ios/documentation/SpriteKit/Reference/SKPhysicsWorld_Ref/Introduction/Introduction.html#//apple_ref/occ/instp/SKPhysicsWorld/speed ) – AndrewShmig Mar 09 '14 at 12:22
  • 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; – μ4ρκ05 Mar 09 '14 at 12:24
  • scene.physicsWorld.speed = 0.0 did the job well! Thanks! – μ4ρκ05 Mar 09 '14 at 12:28

2 Answers2

23

Try setting your physicsWorld.speed to 0.0:

scene.physicsWorld.speed = 0.0

Apple SKPhysicsWorld Ref.

AndrewShmig
  • 4,843
  • 6
  • 39
  • 68
  • How do you resume later? What speed to you reset to when you want to resume? – Peter Peng Apr 09 '17 at 02:49
  • 2
    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. – Peter Peng Apr 09 '17 at 03:08
3

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

self.scene.view setPaused:YES

Amani Elsaed
  • 1,558
  • 2
  • 21
  • 36