How to restart a level in an iPhone game - cocos2d-iphone

I want to restart the current scene i am on. I thought of using replaceScene and replace it with itself. Is that ok to do ?
Level2Scene *scene = [Level2Scene node];
[[CCDirector sharedDirector] replaceScene:scene];
Level2Scene is the current scene I am in.
Thanks
Abhinav

Using replaceScene: is a good way to restart the level, if levels are represented by scenes. Just make sure that if you have any global state (stored outside of the scene) that you reset that, too.

Related

Change scene in cocos2d

i m using cocos2d and i want to change scene to replay the game when the player touches the end scene so that it starts the game again.
I just stuck to use touch event on [[CCDirector sharedDirector]replaceScene:[CCTransitionZoomFlipAngular transitionWithDuration:2 scene:scene orientation:kOrientationLeftOver]];
Please Help
Thanks in advance
Don't ever send an existing, running scene to be replaced. Create a new instance of the same scene class if you want to restart the scene.

Why am I not able to Add new Layer to Scene from another Layer by parent?

I don't get why this code is incorrect...
DeadPanelLayer* deadPanelLayer = [DeadPanelLayer node];
[(CCScene*)self.parent addChild:deadPanelLayer z:2];
DeadPanel is a layer that I want to add on the Scene running from another layer
any idea why my layer is not loaded?
Edit: the game is not crashing, but the new layer does't show up. I tried to even move the objects on the scene and layer just to make sure it is not displaying due zindex hierarchic
but still...
I tried to add a method on Scene to add the layer in case it is called from parent:
[(GameScene*)self.parent showDeadPanel];
and even get current scene from Director
[[CCDirector sharedDirector] runningScene]
and it doesn't work either

what causes a scene's schedule to run even after the scene is replaced?

i have a scene1 that is replaced by scene2
[[ccdirector shareddirector]replacescene:scene2]//scene2 replacement
after doing some selection on scene2 i am again replacing the scene
again with scene1
[[ccdirector shareddirector]replacescene:scene1] //scene1 replacement
but scene2's schedule is still running, and i havent really retained
anything in scene2, pls help me with this!!
This can only happen if your scene isn't released. That means, yes, you do retain it somehow. Keep in mind that adding an object to an NSArray or NSDictionary retains it.
Set a breakpoint in each scene's -(void) dealloc method, or add an NSLog/CCLOG line, to make sure the scene is properly deallocated.
I'm guessing from your code that you're actually holding on to the scene1 and scene2 objects. You should not do that. Instead, create a new instance of the scene class every time you change it, like so:
[[CCDirector sharedDirector] replacescene:[Scene2 node]];

Cocos2d - a way to get the current scene?

I'm looking for a way to get the current scene so that I'll be able to tell which scene is running at any time.
Thanks!
Check out CCDirector. You can get the running scene like this:
[[CCDirector sharedDirector] runningScene];
From the documentation of cocos2D:
-(CCScene*) runningScene [read, assign]
The current running Scene. Director can only run one Scene at the time
Sandro Meier

how can we know that we have released memory

I have created a game of two levels.Now when we are in level one i create sprite sheet for animation and a lot of sprites.On reaching a certain score i move to level 2 now here here is another sprite sheet and a lot of variables.
When i am moving from level 1 to level 2 using.
[[CCDirector sharedDirector] pushScene:[Level2 node]];
when i lose on level 2 i move back to level 1 using
[[CCDirector sharedDirector] pushScene:[Level1 node]];
What happens to the sprite sheet and other sprites i created before on level 1 ? If i retry level 1 will the sprite sheet and sprites i created before be removed automatically ?or they will exists in this new scene?
kindly clear me these issue i am having a lot of trouble because of no understanding of this..
thank you in advance.. :(
In cocos2d almost everything is marked as autorelease. So when you create a new scene it is not released because it becomes the main scene. Once it is no longer the main scene, if you do not specifically retain it then it will be released.
In the case you state above I believe that both scenes are retained as you are using the director like a stack, pushing the scenes on each other. If you never need to go back to level 1 you can use CCDirector's replaceScene: method instead to release level 1.
Having a custom pause scene or bonus round might be a good example of when to use the pushScene: call.