如何在 runAction 结束时调用特定函数?

发布于 2024-12-13 03:06:14 字数 705 浏览 4 评论 0原文

我正在对精灵执行 runAction 以移动到某个位置。

[[crBalls[cb.count] getball] runAction:[CCSequence actions:[CCMoveTo actionWithDuration:timeToTravel position:ccp(xIntercept,yIntercept)],[CCMoveTo actionWithDuration:0.05 position:FD],nil]];

一旦精灵移动到所需的位置(FD),我想在那时调用一个函数。现在我正在安排一个选择器在“timeToTravel”延迟后调用,这是上述操作完成执行所需的时间。(我使用调度程序而不是执行选择器,因为执行选择器更容易出现问题)

[self schedule:@selector(placeThatDamnBall) interval:timeToTravel+0.05];

-(void) placeThatDamnBall
{
[self unschedule:@selector(placeThatDammBall)];
[self ballPlacedIn:FD.x :FD.y :cb.type : cb.count];
}

但这是并不完全可靠,并且在极少数情况下可能会导致问题,即该函数可能会在精灵到达目的地之前被调用。有什么方法可以避免调用选择器并能够在精灵真正到达目的地后调用该函数吗?

谢谢

I am performing a runAction on a sprite to move to a position.

[[crBalls[cb.count] getball] runAction:[CCSequence actions:[CCMoveTo actionWithDuration:timeToTravel position:ccp(xIntercept,yIntercept)],[CCMoveTo actionWithDuration:0.05 position:FD],nil]];

Once the sprite moves to the desired position(FD) then I want to call a function at that time. Right now I am scheduling a selector to be called after a delay of 'timeToTravel' which is the time taken by the above action to finish performing.(I am using scheduler instead of performSelector since perform selector is more prone to problems)

[self schedule:@selector(placeThatDamnBall) interval:timeToTravel+0.05];

-(void) placeThatDamnBall
{
[self unschedule:@selector(placeThatDammBall)];
[self ballPlacedIn:FD.x :FD.y :cb.type : cb.count];
}

But this is not entirely reliable and may cause problem in a rare case where the function might get called before the sprite reaches the destination. Is there any way I can avoid having to call a selector and be able to call the function once the sprite has truly reached the destination?

Thanks

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

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

发布评论

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

评论(1

第七度阳光i 2024-12-20 03:06:14

在序列末尾添加一个 CCCallFunc

[[crBalls[cb.count] getball] runAction:
    [CCSequence actions:
        [CCMoveTo actionWithDuration:timeToTravel position:ccp(xIntercept,yIntercept)],
        [CCMoveTo actionWithDuration:0.05 position:FD],
        [CCCallFunc actionWithTarget:self selector:@selector(placeThatDamnBall)],
        nil]];

Add a CCCallFunc at the end of your sequence:

[[crBalls[cb.count] getball] runAction:
    [CCSequence actions:
        [CCMoveTo actionWithDuration:timeToTravel position:ccp(xIntercept,yIntercept)],
        [CCMoveTo actionWithDuration:0.05 position:FD],
        [CCCallFunc actionWithTarget:self selector:@selector(placeThatDamnBall)],
        nil]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文