如何知道SKSpriteNode是否未运动或停止?

时间:2023-01-23 00:26:11

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

在我的游戏中,我需要在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上施加力,因此它会因为作用在它上面的力而开始移动。现在,我需要知道它什么时候停止移动。

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

我知道SKSpriteNode有一个speed属性,但默认情况下总是设置为1.0。

Any ideas?

3 个解决方案

#1


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

由于您使用物理学,您可以使用SKPhysicsBody的静止属性。

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

Hope this helps.

希望这可以帮助。

#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.

放置上述代码的通常位置是更新方法。

#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:

您可以子类化SKSpriteNode并在其上添加previousPosition属性,您可以在-didFinishUpdateMethod上更新它。如果位置没有变化,您可以假设节点已停止(您可能必须获得最后几个位置的中位数以使其平滑)。速度属性是不同的,根据类参考,速度是:

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

速度修饰符应用于节点及其后代执行的所有操作。

Hope this helps! Danny

希望这可以帮助!丹尼

#1


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

由于您使用物理学,您可以使用SKPhysicsBody的静止属性。

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

Hope this helps.

希望这可以帮助。

#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.

放置上述代码的通常位置是更新方法。

#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:

您可以子类化SKSpriteNode并在其上添加previousPosition属性,您可以在-didFinishUpdateMethod上更新它。如果位置没有变化,您可以假设节点已停止(您可能必须获得最后几个位置的中位数以使其平滑)。速度属性是不同的,根据类参考,速度是:

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

速度修饰符应用于节点及其后代执行的所有操作。

Hope this helps! Danny

希望这可以帮助!丹尼