Cocos2d - a way to get the current scene? - cocos2d-iphone

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

Related

How to show particle from a thread in Cocos2d

I am trying to take a particle effect in my Cocos2d project. Particle effect shows good. But I am confused when I put the particle showing function into a thread as follows, it only shows tiny dark squares instead of the right texture. thanx in advance.
// thread caller
[self performSelectorInBackground:#selector(showParticleThrd:) withObject:nil];
// it works good when i call it directly
-(void) showParticleThrd{
CCParticleSystem * system = [[ParticleEffectSelfMade alloc] initWithTotalParticles:1];
[aLayer removeChildByTag:R_TAG cleanup:YES];
system.position = ccp(self.position.x,self.position.y);
[self.parent addChild:system z:R_ORDER tag:R_TAG];
[system release];
}
You can not modify anything cocos2d related in a background thread. Cocos2d requires you to make changes to nodes in the main thread, where the OpenGL context was created.

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

Cocos2d Parallax Loop

I've searched all of the forums and cannot find a working solution to get my parallax layer to loop. YES - I've tried all of the tutorials, includine the Space shooter by Ray Wenderlich but I'm struggling. Here's the code:
CCParallaxNode *parallax = [CCParallaxNode node];
// My Parallax Layer
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
CCSprite *midground = [CCSprite spriteWithFile:#"trees.png"];
midground.anchorPoint = ccp(0,0);
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_Default];
[parallax addChild:midground z:-9 parallaxRatio:ccp(1.4f, 1.4f) positionOffset:ccp(0,0)];
//Please loop once off screen
The image is 960x640 and i would like it to update and loop once it leaves the page. Any help much appreciated.
Try this simple solution:
http://www.gomonkey.it/2012/02/cocos2d-scorrimento-del-background/
You have need 2 images

How to restart a level in an iPhone game

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.