如何在移动动作期间更改 CCSprite 的位置

发布于 2024-11-19 20:42:30 字数 252 浏览 2 评论 0原文

是否可以在移动操作期间更改 CCSprite's 位置,例如 CCMoveBy? 我有一个 CCSprite 的子类,它循环执行一系列操作,其中一些是动作。我检查它是否已经离开屏幕,然后尝试将其直接移动到屏幕的另一侧。因此,如果它在向左移动的过程中离开屏幕左侧,它将从屏幕右侧完成移动。

但是,如果我使用 setPosition: 它似乎不起作用,它只是完成了移动操作。

Is it possible to change a CCSprite's position during a move action, like CCMoveBy? I have a subclass of CCSprite that cycles through a bunch of actions, some of which are moves. I check to see if it has gone offscreen, and then attempt to move it directly to the opposite side of the screen. So if it went offscreen left halfway through a move to the left, it would finish the move from the right side of the screen.

But if I use setPosition: it doesn't seem to work, it just finishes it's move action.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我为君王 2024-11-26 20:42:30

CCMoveToCCMoveBy 是其子类)存储对象的初始位置,并在每次更新时向该存储位置添加增量,因此任何其他位置设置都将是毫无意义,因为下一次更新会将其放回旧路径。

您可以更改代码以使用两个 CCMoveTo 操作 - 一个将对象移出屏幕,另一个将其从另一侧移回屏幕。或者,您可以创建自己的操作(查看 CCMoveTo 的源代码(如果您不确定这是如何完成的)。

CCMoveTo (which CCMoveBy is a subclass of) stores the initial position of the object and adds a delta to that stored position at each update, so any other setting of position would be pointless as the next update would put it back on its old path.

You can change your code to use two CCMoveTo actions - one taking the object off screen, and another to move it back in from the other side. Alternatively, you can create your own action (take a look at the source code for CCMoveTo if you're unsure how that's done).

想你的星星会说话 2024-11-26 20:42:30
[sprite stopAllActions];
sprite.position = rightSideOfScreen;
[sprite runAction:newMoveToLeftAction];

我期望这应该有效。如果您确实需要“继续该操作”而不是开始新的操作,那么您必须获取该操作运行了多长时间,并以应该剩余的时间开始新的操作。

[sprite stopAllActions];
sprite.position = rightSideOfScreen;
[sprite runAction:newMoveToLeftAction];

That should work I expect. If you really need to "continue the action" and not start a new one then you will have to obtain how long the action was running and start the new action with the amount of time that should have been left.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文