Sprite 套件中的停止按钮 objective c

Stop button in sprite kit objective c

我在 sprite kit 中构建了一个游戏,我的所有 sksprite 节点都随着力的增加而移动 cgvector。 我想在游戏中添加一个停止按钮,但如果我停止所有节点,我将无法 return 他们拥有的力量。所以我无法停止游戏并从同一点再次玩。 所以有人知道如何在节点上保存力吗?或者我怎样才能停止按钮? 谢谢。

您拥有的 SKPhysicsWorld 对象有一个名为 speed 的 属性。

来自 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.

因此,基本上您可以将力施加到所有节点,但有效地 "pause" 物理模拟。


或者,您可以使用 paused 属性 来停止所有动作(不仅仅是使用力的物理动作)

所以如果你使用速度 属性:

self.physicsWorld.speed = 0.0;

如果你有很多事情要做,或者如果你想制作一种逐渐减慢速度以获得慢动作效果的方法,这将非常有用。由于您不必在 sprite kit 中实际制作 physicsWorld 对象(它在制作场景时被初始化),这就是您所要做的。

暂停一切也不是很复杂:

self.scene.view.paused = YES;

使用SKView的暂停属性:

self.scene.view.paused = YES;

这将停止所有动作和物理模拟。

如回答here.

一个 SKSpriteNode 有一个名为 userData 的字典 属性。您可以将 dx 和 dy 速度存储在那里以供以后检索。

如果视图中的所有节点都停止移动,您可以使用以下方法枚举所有节点:

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

如果只有您的一些节点需要经历速度变化,您需要将每个受影响的节点添加到一个数组并枚举该数组。