Poppy Deejay
Poppy Deejay

Reputation: 85

cocos2d - CCDirector, the replaceScene function issue

so i'm currently working on an app that draws something on the screen, like a graph and things like that. while testing it and it's functionality i came across this issue.

i have a button that when pressed goes to a settings scene. i initialize the scene like this

+(id) scenew
{
   CCScene *scene = [CCScene node];
   Settings *layer = [Settings node];    
   [scene addChild: layer]; 
   return scene;

}

in that scene i have two more buttons, a done button and a what's new button.

done = [CCMenuItemImage itemFromNormalImage:@"done.png" selectedImage:@"done.png" target:self selector:@selector(done:)];

pressing the what's new button goes instantly but the done button has a 2-3 seconds delay. the app has some console messages appearing and it seems that when it is pressed it recalculates the whole graph just like when starting the app. all the buttons call the same CCDirector function which is replaceScene.

-(void) whatNew: (id)sender
{
[[CCDirector sharedDirector] replaceScene: [New scene]];
}

-(void) done: (id)sender
{
[[CCDirector sharedDirector] replaceScene: [Main scene]];
}

is there a way for me to optimize this a little...i mean...an efficient way to use the replaceScene, or maybe something else that doesn't force the recalculation of the graph? because whenever that button is pressed it practically jumps at the top of my graph class implementation which has 4500+ lines O.o

Upvotes: 2

Views: 1122

Answers (1)

Lukman
Lukman

Reputation: 19164

I assume the first button you mentioned above is inside the Main scene, going to the Setting scene.

To retain the Main scene in the memory while transferring the app flow to the Setting scene, all you have to do is to use pushScene: instead of replaceScene::

// in Main scene class
[[CCDirector sharedDirector] pushScene:[Setting scene]];

Then, from inside Setting scene, simply use popScene to return to the Main scene:

-(void) done: (id)sender
{
    [[CCDirector sharedDirector] popScene];
}

Basically pushScene: simply push the new scene on top of the current scene while popScene pops it out, reverting the app flow back to the previous scene that still remains in the device memory.

Upvotes: 2

Related Questions