How to pause and resume in Cocos2d
Therefore, I have a special bullet that freezes enemies, and after a while the enemies unfreeze and continue their actions / animations. Here's a simple version of what I did:
-(void)update:(ccTime)dt
{
CCSprite *enemySprite;
CCARRAY_FOREACH(enemies, enemySprite)
{
if (CGRectIntersectsRect(_bullet.boundingBox, enemySprite.boundingBox))
{
_bullet.visible = NO;
[enemySprite pauseSchedulerAndActions];
enemySprite.pausingDuration = CACurrentMediaTime() +5;
}
if (CACurrentMediaTime() > enemySprite.pausingDuration)
[enemySprite resumeSchedulerAndActions];
}
}
Now the problem I am running into is that the enemySprite has stopped updating its scheduler here, so the next time the update method is called by the enemy the suspended mode will not receive the update! I wish I knew a better way to explain this, but I think any expert programmer will immediately see what is wrong with this code. Please help me with suggestions for improving the code or even just an idea would be appreciated, thanks for your time.
source to share
Have you called? :)
Yes, pauseSchedulerAndActions as well as control pause methods are both cheesy ways to implement pause because you have no control over what is paused and what can be continued (for example, perhaps the pause menu layer).
In your case, you can at least be more specific and, for example, only suspend activities, but not scheduled updates:
[enemySprite.actionManager pauseTarget:enemySprite];
For finer grained control, it is advised not to rely too much on scheduled methods in each object, instead the central object (scene or layer) sends all updates to its children - this way you can more easily decide later which children should keep receiving updates during pause ...
source to share