如何实际暂停cocos2d计划选择器? [英] how to actually pause a cocos2d scheduled selector?

查看:103
本文介绍了如何实际暂停cocos2d计划选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可能有成千上万的人问过这个。但我的问题是有点不同。我想知道是否有一个实际的方式来暂停一个计划的选择器,不只是取消调度它。我需要知道这是因为我有一个选择器被呼叫每50秒。如果我要取消调度该选择器,剩下2秒钟,然后重新安排它,那么该函数将需要98秒来调用该函数。

I know there is probably thousands of people who asked this before. But my question is quite a bit different. I'm wondering if there is an actual way to pause a scheduled selector not just unschedule it. I need to know this because i have a selector being called every 50 seconds. if i were to unschedule that selector with 2 seconds left, then reschedule it, then the function would take 98 seconds to call the function.

推荐答案

您可以暂停节点(目标,本例中为self)的所有选择器:

You can do this to pause all selectors for a node (the target, in this case self):

[[CCScheduler sharedScheduler] pauseTarget:self];

如果你不能使用,你必须自己跟踪时间。在这种情况下,可能最容易安排更新选择器:

If you can't use that, you will have to keep track of time yourself. In that case it's probably easiest to just schedule the update selector:

[self scheduleUpdate];

然后写入更新方法:

-(void) update:(ccTime)delta
{
    totalTime += delta;
    if (isSelectorXPaused == YES)
    {
        nextUpdateForSelectorX += delta;
    }
    else if (totalTime > nextUpdateForSelectorX)
    {
        nextUpdateForSelectorX = totalTime + 50;
        [self performX];
    }
}

变量totalTime,isSelectorXPaused和nextUpdateForSelectorX是实例变量。如果名为X的选择器暂停,则下一次应该运行的选择器只是前进了已经过去的时间,本质上这使得当选择器暂停时totalTime和nextUpdateForSelectorX之间的差异保持不变。

The variables totalTime, isSelectorXPaused and nextUpdateForSelectorX are instance variables. If the selector named X is paused, the next time it should run is simply advanced by the time that has elapsed, essentially this keeps the difference between totalTime and nextUpdateForSelectorX constant while the selector is paused.

如果选择器未暂停,并且更新到期,则nextUpdateForSelectorX将在未来前进50秒,并使用常规消息发送执行选择器。

If the selector isn't paused and an update is due, the nextUpdateForSelectorX is advanced 50 seconds into the future and the selector is performed using regular message send.

这是基本原则,你应该能够扩展这个支持多个选择器。已经省略了变量的初始化,实际上暂停选择器。它不应该造成很大的问题。

This is the basic principle, you should be able to extend this to support multiple selectors. Initialization of the variables has been left out, as has actually pausing the selector. It should not pose a big problem.

这篇关于如何实际暂停cocos2d计划选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆