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