0

I build a game in sprite kit and all my sksprite node moving with adding force cgvector. I want to add a stop button to the game but if I will stop all the nodes I will not be able to return the force they had. so i can't stop the game and play again from the same point. So somebody know how its possible to save a force on nodes? Or how I can to the stop button? thanks.

sangony
  • 11,636
  • 4
  • 39
  • 55
100tomer
  • 137
  • 1
  • 10

3 Answers3

1

The SKPhysicsWorld object that you have has a property called speed.

from Apple.com:

The default value is 1.0, which means the simulation runs at normal speed. A value other than the default changes the rate at which time passes in the physics simulation. For example, a speed value of 2.0 indicates that time in the physics simulation passes twice as fast as the scene’s simulation time. A value of 0.0 pauses the physics simulation.

So, essentially you could keep the force applied to all your nodes but effectively "pause" the physics simulation.


Alternatively, you could use the paused property that will stop ALL actions (not just the physics ones using force)

So if you would use the speed property:

self.physicsWorld.speed = 0.0;

this works pretty nice if you have quite a bit going on, or if you want to make a method that incrementally slows down things for slow-motion effects. Since you don't have to actually make the physicsWorld object in sprite kit (it is initialized when the scene is made), this is all you have to do.

And pausing everything isn't very complicated either:

self.scene.view.paused = YES;
MaxKargin
  • 1,560
  • 11
  • 13
0

Use SKView's paused property:

self.scene.view.paused = YES;

This will stop all actions and physics simulation.

As answered here.

Community
  • 1
  • 1
Joaquin Llaneza
  • 451
  • 2
  • 15
0

A SKSpriteNode has a dictionary property called userData. You can store your dx and dy velocity there for later retrieval.

If all your nodes in the view are to stop moving, you can enumerate through all of them using:

for(SKSpriteNode *object in self.children) {
    // set velocity of each node
}

If only some of your nodes need to undergo the velocity change, you would need to add each one of the effected nodes to an array and enumerate that array.

sangony
  • 11,636
  • 4
  • 39
  • 55