Sami Farhat
Sami Farhat

Reputation: 1162

Cocos2d: Schedule methods unresponsive after replacescene is called for the SECOND time

First time method "StartGame:" in Menu.h is launched, the game works impeccably.

However during playtime, if user goes back to Menu- I use

[[CCDirector sharedDirector]replaceScene:[Menu scene]];

all type of animation freezes and schedule functions are unresponsive.

What the heck is going on ?

Upvotes: 0

Views: 592

Answers (3)

Scott
Scott

Reputation: 168

I, too, was having the problem (in v3.1.1) and am not overriding any of the onEvent handlers, so calling super (as suggested by @LearnCocos2D) is not applicable here.

I DID find a fix, though. Calling the director's resume function, either before or after the call to replaceScene, fixes the problem for me.

[[CCDirector sharedDirector] replaceScene:scene];
[[CCDirector sharedDirector] resume];

Upvotes: 0

PKCLsoft
PKCLsoft

Reputation: 1358

I'm not sure if this is the same issue, but what I've found recently is that if I want to use replaceScene to swap between two scenes where I want to keep the "old" scene as a singleton that can be swapped back to later, the call to replaceScene ends up calling "cleanup" on that scene which leaves the scene intact except that any buttons that have handlers in the form of blocks or selectors (that get wrapped by blocks internally) lose their handlers and become unresponsive when you swap back to the "old" scene.

I've done this so that the app isn't always creating entirely new scene objects (along with all of their children) every time the user goes from the main menu to another part of the app.

To get around this, I either had to redesign so that scenes are always created anew every time there's a transition, or to add a tweak to CCDirector:

-(void) replaceScene:(CCScene*) scene andCleanup:(BOOL)andCleanup

What I've done so far is to add an override to CCDirector.replaceScene that accepts a cleanup parameter (similar to other removeAndCleanup messages). This allows the app in question to retain scenes when it wants to without them being cleaned when the app doesn't want them cleaned.

The implementation for this is:

-(void) replaceScene: (CCScene*) scene
{
    [self replaceScene:scene andCleanup:YES];
}

-(void) replaceScene:(CCScene*) scene andCleanup:(BOOL)andCleanup
{
    NSAssert( scene != nil, @"Argument must be non-nil");

    NSUInteger index = [scenesStack_ count];

    sendCleanupToScene_ = andCleanup;
    [scenesStack_ replaceObjectAtIndex:index-1 withObject:scene];
    nextScene_ = scene; // nextScene_ is a weak ref
}

If someone thinks this is a dumb way to go, please let me know.

Upvotes: 2

CodeSmile
CodeSmile

Reputation: 64477

Did you override any of these methods?

 -(void) onEnter;
 -(void) onEnterTransitionDidFinish;
 -(void) onExit;
 -(void) cleanup;

If so, make sure you call the super implementation (ie [super onEnter];) inside each method, otherwise all kinds of odd things can occur.

Upvotes: 2

Related Questions