how to actually pause a cocos2d scheduled selector?

感情迁移 提交于 2019-12-13 02:59:31

问题


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.


回答1:


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];

Then write the update method:

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

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.

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.



来源:https://stackoverflow.com/questions/8538323/how-to-actually-pause-a-cocos2d-scheduled-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!