重新启动 iPhone 版 cocos2D 中的操作

发布于 2024-09-14 03:43:52 字数 367 浏览 3 评论 0原文

如果我使用的操作在 cocos2D 中不重复,如何重新启动该操作?

我正在使用代码:

 CCAnimate* action = [CCAnimate actionWithAnimation:myAnimation];

 [mySprite runAction: action];

该操作一次运行良好,但是当触发事件时,我希望能够在完成后再次运行该操作,所以我尝试在触发事件时使用它。

 if( [action isDone] ){

     [mySprite runAction: action];

 }

但这会导致崩溃。有人知道执行此操作的正确方法是什么吗?

If I am using an action that does not repeat in cocos2D how do I restart that action?

I am using the code:

 CCAnimate* action = [CCAnimate actionWithAnimation:myAnimation];

 [mySprite runAction: action];

The action runs fine once, but when an event is triggered I want to be able to run the action again as long as it is finished, so I tried using this when the even is triggered.

 if( [action isDone] ){

     [mySprite runAction: action];

 }

But this results in a crash. Anyone any idea what the correct way to do this is?

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

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

发布评论

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

评论(3

七色彩虹 2024-09-21 03:43:52

尝试将操作保留在实例变量中。在头文件中声明一个指针,

CCAction* myAction;

然后在层或精灵初始化时

myAction = [CCAnimate actionWithAnimation:myAnimation];

从什么时候开始,每当您想要调用您的操作时,

if( [action isDone] ){

 [mySprite runAction: myAction];

}

我认为您的应用程序崩溃的原因是因为您正在调用仅在持续时间内存在的操作它被初始化的方法。

在我的游戏中,我使用 CCSequences(因此我可以使用 CCCallFunc 在动画中设置/声明变量),所有这些 CCSequences 都作为实例变量存储在我的 CCSprite 子类中。

我有一个永远重复的空闲动画。

每当我想要“跳跃”时,例如我会调用

[self stopAllActions];
[self runAction:jumpSeq];

My JumpSeq 是一个播放跳跃动画的 CCSequence,并且在序列末尾有一个 CCCallFunc,用于在完成后重新启动空闲动画。

希望这有帮助。

进一步阅读: http://www.cocos2d -iphone.org/wiki/doku.php/prog_guide:actions_special?s[]=cccallfunc

try preserving the action in an instance variable. In the header file have a pointer declared

CCAction* myAction;

then when the layer or sprite is initialized

myAction = [CCAnimate actionWithAnimation:myAnimation];

From what point on, whenever you want to call your action do

if( [action isDone] ){

 [mySprite runAction: myAction];

}

I think the reason your app is crashing is because you are calling an action that only exists for the duration of the method in which it is initialized.

Im my game i use CCSequences (so i can use CCCallFunc to set/declare variables mid animation), all these CCSequences are stored as instance variables in my CCSprite subclass.

I have an idle animation that repeats for ever.

Whenever I want to 'jump' for instance i call

[self stopAllActions];
[self runAction:jumpSeq];

My jumpSeq is a CCSequence that plays a jump animation, and has a CCCallFunc at the end of the sequence that restarts the idle animation when it is done.

Hope this helps.

Further reading: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:actions_special?s[]=cccallfunc

凉世弥音 2024-09-21 03:43:52

结果我只是没有保留该动作,并且精灵一定在完成后删除了它。所以现在我的代码是;

  CCAnimate* action = [CCAnimate actionWithAnimation:myAnimation];

  [mySprite runAction: action];

  [action retain];

然后当我想让它再次运行时;

 if( [action isDone] ){

      [mySprite runAction: myAction];

 }

Turns out I just wasn't retaining the action and the sprite must have been deleting it once it was done. So now my code is;

  CCAnimate* action = [CCAnimate actionWithAnimation:myAnimation];

  [mySprite runAction: action];

  [action retain];

and then when i want it to run again;

 if( [action isDone] ){

      [mySprite runAction: myAction];

 }
爱你不解释 2024-09-21 03:43:52

我遇到了同样的问题,我在头文件中声明了 CCAction* myAction ,但是当我从另一个方法调用它时,我遇到了崩溃,但就像@Bongeh 在使用 [myAction keep] 时提到的那样,它工作得很好

I had the same issue I declared CCAction* myAction in the header file but when i went to call it from another method i experienced a crash but like @Bongeh mentioned when using [myAction retain] it worked perfectly

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