How can a CCLayer subclass be added to a CCMenu? I have a CCLayer subclass that's like a switch control and I'd like to include in a CCMenu.
CCMenu manages subclasses of CCMenuItem. You can try inherit your switch from CCMenuItem and override it's activate method.
Related
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.
If I have a CCSprite that has previously been added to a parent CCNode or CCLayer, is there any way to change its parent object?
I tried doing:
[self setParent:newParent];
...
and then:
[parent removeChild:self cleanup:YES];
[newParent addChild:self];
Neither works... THe latter actually causes a crash.
I assume that in the second case you added autoreleased object to the parent. In this case sprite will be deleted after removing from parent, so you will not be able to add it to another parent. Try this
[self retain];
[self removeFromParentWithCleanup:YES];
[newParent addChild: self];
[self release];
And in the case of crash with error, post the crash message in future. It can help to figure out problem.
I think if you first remove (NO cleanup), then add to new parent, it should work.
I have a CCMenu with CCMenuItems that I add on a CCLayer. When I click on the CCMenuItems, my CCTouchesBegan doesn't fire up.
How can I call this method also when I touch my menu items?
CCMenu registers as targeted touch delegate and swallows touches on menu items. You can try to create your subclass of CCMenu and override it registerWithTouchDispatcher method like this
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:kCCMenuTouchPriority swallowsTouches:NO];
}
This should work as you want, but maybe can cause other problems with menu behavior.
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.