Cocos2D iPhone - adding a CCLayer on top of another - iphone

I am newbie to Cocos2d. I have this layer that represents my main game scene. Lets talk in terms of Cocos2D default template. In this case, my main game scene would be HelloWorldLayer.
Now I want to present a menu. I have create the menu as an individual subclass of CClayer.
How do I make the menu appear using some kind of transition on top of the main scene?
If I use something like
CCScene *menu = [Menu scene];
[[CCDirector sharedDirector] replaceScene:
[CCTransitionCrossFade transitionWithDuration:0.5f scene:menu]];
I would be using the menu as a scene and replacing the main scene with it. This is not what I want. I want to make the menu appear on top of the main scene, using some kind of transition and if possible fading the main scene to 50% or whatever.
How do I do that?
thanks.

Instead of replacing the scene, simply make your Menu class a CCLayer and add it existing scene. You can set opacity and add masking sprites as needed to fade out the bottom layer if you need to.
CCLayer* newLayer = [Menu layer];
[self addChild: newLayer];
You will need to handle much of the details on which layer captures input, but this is the basic idea.

Related

Cocos2d CCFollow when there are multiple layers

Essentially, when I have one CCLayer for background and platform sprites and another for player sprite and HUD, how should I go about calling CCFollow if I want my camera to always center at the player with the HUD in the same layer moving accordingly as well?
Right now the background layer is a subclass of CCLayer that has a CCSpriteBatchNode for drawing objects in the environment. It contains an instance variable CCLayer *HudLayer that has a CCMenu and another CCSpriteBatchNode for the player animation sprites. They are both direct children of a CCScene, meaning HudLayer is not a children of BackgroundLayer. I'm now calling the CCFollow as [self runAction:[CCFollow actionWithTarget:playerSprite worldBoundary:worldRect]] where self is the BackgroundLayer.
The camera follows the box2d physics body of the player correctly, and the menu moves with it too, but whenever the camera moves, let's say to the right, the player sprite moves twice as fast to the right, leaving its physics body behind. So after moving to the right for a bit the playerSprite is outside of screen. playerSprite.pos and playerSprite.position seem to have nothing wrong though.
I've tried calling the CCFollow in different places but either the HUD refuses to move or the camera won't move or the player moves weirdly. What's wrong with it?
I FIGURED IT OUT! The key here is to put the player sprite in some layer other than the HUD layer. I added my CCSpriteBatchNode for the player sprites as a child of backgroundLayer instead of the HudLayer without changing anything else and it worked.
So basically everything that needs to always stay on screen as the HUD should be in one layer, while the target node for CCFollow should be in a separate layer (in the background in my case). The caller layer of CCFollow should contain the target node as a direct child. This tutorial helped me a great deal: http://www.raywenderlich.com/4666/how-to-create-a-hud-layer-with-cocos2d

Control Sprite in another scene from HUD layer

I have a sprite in scene1.m and I have a button in HUD layer hud.m. Now a button appears in Scene1.
How can I get control of scene1 sprite from HUD layer. Any ideas?
With cocos2d, you can assign a tag to your sprite (or button, or any class derived from CCNode), and to each of its hierarchy of parents (see CCNode class, setTag() ). You can then use these tags to retrieve the corresponding nodes from anywhere in your code, with CCNode getChildByTag().

Use CCLabelBMFont inside UIViewController Inside Node

I'am doing a game, when the player lose, and screen of Game Over appears. But my Game Over screen its in a UIViewController. I call it in my Director, and alls right.
The Game over screen appears like overlay in the game
But i am using Custom Fonts, and in my Game Over screen I want to use it. But how could I use only "CCLabelBMFont" in the Game Over.
I think a way (no the better one). Its that you print in a bigger z-index than the Game Over the text I want it with "CCLabelBMFont". But I want to have all the stuff of Game Over in the GameOver.m
What could I do? Thanks to everybody.
To present anything from Cocos2D on top of a UIKit view, you'll have to make the Cocos2D view transparent and introduce a dummy view. You can't achieve this effect with the z order because that affects only the nodes in Cocos2D's OpenGL view.
To make the Cocos2D view transparent you need to change the color depth to 32 bit (RGBA8888) from the default 16-Bit mode in the EAGLView initialization (app delegate). Then setup the view hierarchy with a dummy view, so that you can actually add UIKit views in the background and the Cocos2D view as the foremost view.
The basic approach is to do this in your app delegate didFinishLaunching method:
UIView* dummyView = [[UIView alloc] initWithFrame:[window bounds]];
[dummyView autorelease];
[dummyView addSubview:[CCDirector sharedDirector].openGLView];
rootViewController.view = dummyView;
[window addSubview:rootViewController.view];
// make the cocos2d view transparent:
glClearColor(0.0, 0.0, 0.0, 0.0);
[[CCDirector sharedDirector] openGLView].opaque = NO;
From then on you can use the dummyView to manage the view hierarchy. For example, when you add your game over view to the dummyView, you want to call sendToBack on it so that it is drawn behind the cocos2d view.
The entire process with all things to consider is described in detail in my Learn Cocos2D Game Development book (2nd Edition).

SubViews with Cocos2d

I have a helloworld scene that I would like to add a subview to.
How is this done in cocos2d? I want to add a subview near the lower part of screen.
I want this view to be controlled by the helloworld scene, hiding and showing the subview as needed.
How is this done in cocos2d?
Thanks
Add the subview (probably CCLayer) as a child to the hello world scene.
CCLayer* subview = //initialize
[subview setPosition:ccp(X,Y)];
[self addChild:subview];
You can add a CCLayer as a child to the CCScene or to the main CCLayer inside of it.

out of the scene elements overlap the previous scene during transitions

I am developing a cocos2d based IPhone game where a scene replace another with a Left to Right Transition. The new scene has a background a bit bigger than the scene. That is just because I move it according to the device tilt to create a sort of 3D effect.
Unfortunately during the transition of the scene I can see the background outside the scene overlapping the previous one :(
I have then tried to use the z-order in such a way the previous scene should go on top of the new one bug it does not work the way it should.
do you have any suggestion?
Thank you in advance
I think you need do crop your scenes layer by overriding it's -(void)visit method.
That's how I did it:
- (void) visit {
glPushMatrix();
glEnable(GL_SCISSOR_TEST);
glScissor(cropRect.origin.x,
cropRect.origin.y,
cropRect.size.width,
cropRect.size.height);
[super visit];
glDisable(GL_SCISSOR_TEST);
glPopMatrix();
}
May be you'll need to play a bit with cropRect origin and size values.
Also you may need to add some device orientation changing support