game menu in another class or in game class? - cocos2d-iphone

we are about to finish our app for the iphone.
all the game is in one class helloWorldLayer.mm . using cocos2d.
Now i need to add the game menu.
2 ways.
adding it in the same class on the init method and just call it from there as a sprite with buttons .
make another class to be the gameMenu class and call this class from the delegate with:
[[CCDirector sharedDirector] runWithScene: [gameMenu scene]];
then from the game menu to load the game scene with :
[[CCDirector sharedDirector] replaceScene: [HelloWorldLayer scene]];
is that method ok ? do i have to add something else? release something?
does memory is better with the first or the second ?
thanks a lot !

A little game menu screen scene should be better - mostly for the sake of organization. Having your menu in a separate scene or in the game class itself shouldn't make much difference, but I would still prefer to have it separately. You could also do some fancy scene transition effect.

Related

Handle Button in CCNode from CCScene

I'm creating a game in cocos2d v3. In the CCScene, I add a CCNode that contains all the components for my HUD. In the CCNode, there are CCButtons added. I want to be able to handle the touch of these buttons in my CCScene. Is that possible? And if so, how is it done elegantly?
Code that I tested this morning and is working:
CCNode header file (Header class):
#property (nonatomic, retain) CCButton *goldButton;
I set up the basics of the gold button in the implementation file of the CCNode (positioning, sprite frame, etc).
CCScene implementation file (PlayScene class):
-(void) setup {
_header = [[Header alloc] init];
[_header.goldButton setTarget:self selector:#selector(goldButtonTapped)];
}
Seems a little weird setting the target of the button inside the scene, but it works. I am wondering of the 'best practice', if you will, for this situation.

How to get back to the previous CCScene?

I have one CCScene, in which their is About us ICON. When I tap on it, goes to the next CCScene where the information to display.
Now, what happen
When I touch on the back option of the android phone, Game closed.
what I need
when I click the back, It takes me to the First CCscene, where the Icon of ABOUT us present.
What should I do for this ???
Solution : From what I solve
CCDirector.sharedDirector().popScene();
You can use the CCDirector to push scenes on top of running scenes.
Say you have scene1 and scene2 both instances of CCScene (or a subclass of CCScene) then when the user taps the about icon :
[[CCDirector sharedDirector] pushScene:scene2]; // Assuming scene1 is already running
when the user taps the back button :
[[CCDirector sharedDirector] popScene];
EDIT : According to your comment (and in java this time :)) :
public void onBackPressed() {
super.onBackPressed();
if(backPressFlag==1) {
CCDirector.sharedDirector().popScene();
}
}
And of course this will work only if you pushed the About scene with :
CCDirector.sharedDirector().pushScene(AboutScene.scene());
you can do in this way on clicking back button
CCDirector.sharedDirector().getRunningScene().removeAllChildren(true);
Now create new scene
sceneIndex--;
CCScene scene = CCScene.node();
scene.addChild(backAction());
CCDirector.sharedDirector().replaceScene(scene);
Here backAction() is same as in cocos examples

Adding cclayer to scene -touches goes down

Always the same problem .
I have a scene and i am adding it a CClayer from another class , which is some background with ccmenu on him .
When touching it, touches goes under this layer, and i dont want it .
otherClass *layer=[otherClass alloc]init]; //otherClass returns a cclayer .
[self addChild: layer];
layer is good, and is above my scene , but the touches goes down.
is there a way in cocos2d to enable ONLY touches at the top layer ??
I must change the touches priority now ?
You need to change touch priority and set to swallowsTouches.
To do this, register your layer on touchDispatcher setting these parameters (you can also see CCLayer registerWithTouchDispatcher method as example):
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

How to add global CCLayer which is not affected by scene transitions in Cocos2d?

Is it possible to add global layer in Cocos2d, which is not affected by scene transitions?
As I can see, it should be above all scenes hierarchy.
There's an old and short discussion on Cocos2d forum, but there's no answer:
http://www.cocos2d-iphone.org/forum/topic/8071
UPD. 'by scene transitions' I mean 'by animated scene transitions'.
You can use the notificationNode property of CCDirector to place a CCNode (ie.CCLayer, CCLabel, etc.) that will remain above scenes, even during transitions. Something like this:
CCLayer *layer = [CCLayer node];
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Test" fontName:#"Marker Felt" fontSize:32];
[layer addChild:label];
[[CCDirector sharedDirector] setNotificationNode:layer]; // Layer should be placed here
[layer onEnter]; // Schedule for updates (ie. so that CCActions will work)
It's meant for notification purposes (ads, etc.) so I wouldn't suggest trying to do anything too fancy from this node.
My gut says no, my brain says maybe.
The documentation says "It is a good practice to use and CCScene as the parent of all your nodes."
I can't test this right now, but looking at the inheritence diagram of CCNode, it looks like the logic of CCNode and CCScene differs only by anchor point. So, you might be able to create a CCLayer to use as your root layer, and add two children to it - A root CCScene, and a CCLayer for your GUI (with a higher Z order).
However, scene transitions may still be tricky, as you generally call CCDirector replaceScene, which works on the root scene you give it. If you give it the CCScene child of your root CCLayer, it may not draw the CCLayer and its GUI child. If you give it the root CCLayer, you are in the same situation as before.
I would still give it a try.
You can create a CCLayer subclass and turn it into a singleton. You add it to the scene like any other child node.
Whenever you transition from one scene to another, you can then remove the layer from the old scene and add it to the new scene as a child. This will only work if you don't use scene transition animations.
The alternative is to not use the CCDirector replaceScene method, and instead design your app to run as a single scene that never changes. To "fake" the changing of a scene you will use two layers, one global layer and another layer that contains your current scene nodes. If you want to transition you can animate the layer with CCActions to, for example, slide out of the screen while sliding in a new layer with a different node hierarchy. All you really lose is the convenience of the CCSceneTransition classes for animating scene changes.

UIButton of previous scene overlaps CCSprite

I have a UIButton that moves around randomly on the screen. On clicking on the button, a new scene is loaded that, for now, contains a CCSprite. Here is the code:
//in init
CCSprite *a = [CCSprite spriteWithFile:#"a.png"];
[a setPosition:ccp(0,0)];
[self addChild:a];
Pretty straightforward, and it stumps me why after 'replaceScene' the UIButton of theHelloWorldScene.m is still visible, right on top of the Sprite. Where am I going wrong?
I'm assuming since you're adding a UIButton into a cocos2d Scene you're using the openGLView
something like:
[[[CCDirector sharedDirector] openGLView] addSubView:button];
If this is the case, then before you replace your HelloWorldScene you'll need call something similar to
[button removeFromSuperview]
Where button is the name of your UIButton (in both instances).
A suggestion though would be to use a CCMenu with a CCMenuItem on your HelloWorldScene as UIKit objects don't really mesh very well with Cocos2d.
You really aren't giving enough information. The simplest answer however is that if you do not want the button to be visible anymore then remove the child (the button). You can set a tag on the button and then use [layer getChildByTag:(NSInteger)].