How to get back to the previous CCScene? - cocos2d-android

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

Related

How to replace scenes when clicked on imageview in cocos2d-android

i have started to develope an game app using cocos2d-android game engine, til now had put 4 imageview and a background image to the scene1, now if clicked on 1st imageview it should navigate to the next scene, i googled and youtube for this but did not succeeded, and also there is no more sources for cocos2d android game engine.
Make your image as menuItem and change your scene on the click method of that menuItem as below
CCMenuItem menuItem1 = CCMenuItemImage.item("backbtn.png", "backbtn.png", this,
"onClickMenuItem");
then handle the onClick method of this menuItem as below
public void onClickMenuItem(Object menuItem) {
if (menuItem == menuItem1) {
CCDirector.sharedDirector().popScene();
}
}
Use CCMenu and CCMenuItemImage or CCMenuItemSprite. You can set method that will be called on menu item click.

CCPanZoomController + Touchable/Clickable sprites

I am using CCPanZoomController to make my 'map' (one image) zoomable and pan-able.
On this map I would like to have clickable/touchable sprites, which when clicked change the image in the sprite.
The problem is that when the user pinches the screen (to zoom out/in), they may touch the sprite, which changes the image of the sprite, which is something I don't want.
I had an idea to solve this, but as I'm new to Cocos2d I don't know how to implement it:
I thought that I could detect when the user touches the screen/the sprite, and doesn't move their touch (as if to pinch or pan) through detecting when the user first touches the screen, (transform that initial touch into a coordinate), and then when the user stops touching the screen (turn that into a coordinate), and compare the both, and if their is no change (or very little change) then change the image of a sprite?
How would I go about doing this? Big thanks to anyone who can help!!
So I've been working with CCPanZoomController myself in my game and ran into similar issues as you but with many different aspects such as when they touch a sprite, I didn't want to have the background move with it or I'd want the sprite to not move when the background was zooming. So what I did was to make methods to "turn off" touches for the layer that I didn't want to react and re-enable them once the action in the other layer was done.
I created the following method inside each layer to disable it or enable it for touch which I call from the different touch events.
// Public Method: Allows for disabling touch for this layer and re-enabling it
-(void)enableTouches:(BOOL)enable
{
// Check if the bool value is to enable or disable touches
if (enable) {
// Call for the removal of all touch locations in array in the CCLayerPanZoom instance
[_panZoomLayer removeTouchesFromArray];
// Call the touch dispatcher and add the CCLayerPanZoom back as a delegate for touches
[[CCTouchDispatcher sharedDispatcher] addStandardDelegate:_panZoomLayer priority:0];
CCLOG(#"PanZoomWrapperLayer:enableTouches - LayerPanZoom touches enabled");
} else {
// Call the touch dispatcher to remove the CCLayerPanZoom as a delegate to disable touches
[[CCTouchDispatcher sharedDispatcher] removeDelegate:_panZoomLayer];
CCLOG(#"PanZoomWrapperLayer:enableTouches - LayerPanZoom touches disabled");
}
}
I've found a simple solution to this problem. However it may not suit your needs!
I've subclassed the CCMenu class and overridden the -(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event as follows:
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
[_selectedItem unselected];
_selectedItem = nil;
}
I've set the touchSwallow property of the instance of that new menu to NO.

CCMenuItemImage isn't showing the selected state

The documentation for CCMenuItemImage doesn't actually say what it does.
There are quite a few subclass CCMenuItem.
I've inherited a project that's using it as a button.
CCMenuItem *start;
start = [CCMenuItemImage itemFromNormalImage:[self prefixedImage:#"start button.png"]
selectedImage:[self prefixedImage:#"start button selected.png"]
target:myTarget
selector:#selector(start:)];
It was using the same button for both states.
I modified it to have a different image for the selected state.
I was expecting/hoping that when I touch the item it will be highlighted, and when I release the button it will send my target action (which it does).
(aside: in iOS parlance, i know that highlighted and selected are two different things. But this library does not seem to have that difference.)
So:
Is it intended to use this "menu item" as a button?
When is the selected image of this menu item displayed?
How should I go about making it display as selected?
CCMenuItem is an abstract class from which all of the other menu items inherit so what you did there in the code is technically wrong.
On the other hand you could subclass CCMenuItem to make your own custom class (for example: you can't use a button and a label on it as a menu item, you have to use either the button itself and the label is on top..just for show, or use the label and the button below is...pointless)
Subclassing CCMenuItem and making your own class would fix that problem (i mean you could make a method that would take an image and a string and returns a button)
What you want to do there is this:
CCMenuItemImage *button= [CCMenuItemImage itemFromNormalImage:#"start button.png"
selectedImage:#"start button selected.png"
target:self
selector:#selector(start:)];
CCMenu *start=[CCMenu menuWithItems:button,nil];
start.position=ccp(200,200);
[self addChild:start];
When you put your finger on the menu it will replace the normal image with the selected one, but will only activate of you release it in the boundingbox of the button (aka..you can press on the button, move your finger away from the button and it wont activate).
So in a sence the button is highlighted untill you release your finger, then its selected.
Did that answer your question?
Try this code...
CCMenuItemImage *backbtn = [CCMenuItemImage itemFromNormalImage:#"backbtn.png" selectedImage:#"backbtn_selected.png" target:self selector:#selector(LBback)];
CCMenu *Menu1 = [CCMenu menuWithItems:backbtn,nil];
[Menu1 alignItemsVerticallyWithPadding:15];
Menu1.position = ccp(160, 240);
[self addChild:Menu1];
By the help of this..when you touch on image is shows selected image other wise normal image...:)
and later when your function get called and you want to change its image then you can set like this..
[backbtn setNormalImage:[CCSprite spriteWithFile:#"backbtn_selected.png"]];
The code above is correct.
The image resource for selection was not added to the project, so was not being displayed. It may have output an error message on creation (buried in other output), but did not output error message when tapped.
The silent/safe failure made the user error harder to track down.

game menu in another class or in game class?

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.

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)].