1

In my game, I need to do some setting changes after my SKSpriteNode stops moving.

First, I apply force on my SKSpriteNode, so it starts moving due to the force acting on it. Now, i need to know when it stops moving.

I know that SKSpriteNode has a speed property, but it is always set to 1.0 by default.

Any ideas?

Andriko13
  • 992
  • 1
  • 7
  • 34

3 Answers3

3

You can check a node's velocity by using something like this:

if((self.physicsBody.velocity.dx == 0.0) && (self.physicsBody.velocity.dy == 0.0)) {
    NSLog(@"node has stopped moving");
}

The usual place to put the above code would be in the update method.

sangony
  • 11,636
  • 4
  • 39
  • 55
1

Since you are using physics you can use the resting property of SKPhysicsBody.

if sprite.physicsBody?.resting == true {
    println("The sprite is resting")
} else {
    println("The sprite is moving")
}

Hope this helps.

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
0

You can subclass SKSpriteNode and add a previousPosition property on it, which you can update on -didFinishUpdateMethod. If there is no change in the position you can assume the node is stopped (you might have to get a median of the last few positions to smooth it out). The speed property is something different, according to the class reference, speed is:

A speed modifier applied to all actions executed by a node and its descendants.

Hope this helps! Danny

Danny Bravo
  • 4,534
  • 1
  • 25
  • 43
  • 1
    Isn't this overkill? You can simply check the sprite's physicsBody's velocity as one of the other answers posted without doing any subclassing, finding median, or running extra methods in the update loop. – Andriko13 May 03 '15 at 00:18
  • Aha! Yes Andriko, you are right! Thanks for the insight! – Danny Bravo May 03 '15 at 06:24