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)].
Related
Since I'm having performance issues with using a UIView parallaxing images in the background of a transparent Cocos2D scene (it was fine in iOS 6), I think I may use the notification node to run the parallax since I need the parallax running between scene transitions. I've tested setting the notifications node, but I haven't figured out how to make it display in a different z-order. I want it to display under everything in the scene.
Right now for my test I have
CCLayer *layer = [CCLayer node];
CCLabelTTF *labelTest = [CCLabelBMFont labelWithString:#"test" fntFile:#"readySetGo.fnt"];
labelTest.position = CGPointMake(100,100);
[layer addChild:labelTest];
[[CCDirector sharedDirector] setNotificationNode:layer]; // Layer should be placed here
[layer onEnter]; // Schedule for updates
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
I am using a UISwitch in my cocos2d project like so:
//header file
UISwitch *musicCtrl;
//implementation file
musicCtrl = [[UISwitch alloc] initWithFrame:CGRectMake(100, 50, 0, 0)];
musicCtrl.on = YES;
[musicCtrl addTarget:self action:#selector(musicOnOff) forControlEvents:UIControlEventValueChanged];
[[[CCDirector sharedDirector]openGLView] addSubview:musicCtrl];
musicCtrl.transform = CGAffineTransformMakeRotation(M_PI/2);
[musicCtrl release];
I have attached the UISwitch to the openGLView but I need to be able to attach it to a certain CCLayer on the GLView. Is that possible? I can't seem to find a UISwitch alternative for cocos2d.
It's not possible.
You can only add UIKit views to the cocos2d view, or the main window. Either way, the UIKit view will be drawn above all of the cocos2d nodes. You can not add a UIKit view to a specific layer.
Specifically you can't add UIKit views to a cocos2d app in a way where some cocos2d nodes are drawn in front of and other nodes drawn behind the UIKit view. That is impossible.
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.
I try to search a button such as UIButton in cocos2d..
UIButton can press several buttons at same time.
but CCmenuitem can't..
Is there anyclass in cocos2d such as uibutton?
Extend CCMenu to support multitouch. It was designed to use targeted touches, that is, single touch interactions. With a little elbow grease, it wouldn't be difficult at all to extend it to support multitouch.
CCMenuItems are used instead of UIButton in cocos2d. A single CCMenuItem inside your CCMenu can exactly work like the UIButton.
You can create that in the following way.
//inside your .m file
-(id) init
{
if( (self=[super init] )) {
CCMenuItem *yourMenuItem = [CCMenuItemImage itemFromNormalImage:#"normalImage.png" selectedImage:#"selectedImage.png" target:self selector:#selector(menuItemPressed:)];
yourMenuItem.position = ccp(60, 60);
CCMenu *yourMenu = [CCMenu menuWithItems:yourMenuItem, nil];
yourMenu.position = CGPointZero;
[self addChild:yourMenu];
}
}
There's a good article by Ray Wenderlich on this here.
However, if your main requirement is to add a UIButton only then this discussion might be helpful.
I would also take a look at SneakyInput if you are having trouble setting up your own buttons; it's very easy to use.