找到你要的答案

Q:How to know whether an SKSpriteNode is not in motion or stopped?

Q:如何知道一个skspritenode不运动或停止?

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?

在我的游戏,我skspritenode停止运动,我需要做一些设置的变化。

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.

我知道SKSpriteNode有一个速度的性能,但它始终是默认设置为1。

有什么想法?

answer1: 回答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.

因为你使用的是物理可以使用skphysicsbody休眠特性。

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

希望这有助于。

answer2: 回答2:

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.

您可以检查节点的速度,通过使用这样的东西:

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

通常的地方,把上述代码将在更新方法。

answer3: 回答3:

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

你可以子类化和SKSpriteNode加一个沿属性,你可以在didfinishupdatemethod更新。如果位置没有变化,你可以假设该节点被停止(你可能需要得到最后几个位置的中位数来平滑)。速度属性是不同的,根据类的引用,速度是:

应用于节点执行的所有动作的速度修饰符。

Hope this helps! Danny

ios  objective-c  sprite-kit  game-physics  skspritenode