Cocos2D CCMenuItem over an other CCLayer doesn't respond
Here is m开发者_高级运维y problem. I got a classic CCLayer subclass. In the init method, I create a CCMenuItem, and add it to my main layer :
CCMenuItemFont *back = [CCMenuItemFont itemFromString:@"back" target:self selector:@selector(back)];
[back setPosition:CGPointMake(30, 30)];
[self addChild:back];
I don't understand why, the method 'back' is not called.
Thanks in advance
You need to add your menu items to a CCMenu, not directly to your layer.
CCMenuItemFont *back = [CCMenuItemFont itemFromString:@"back" target:self selector:@selector(back)];
[back setPosition:CGPointMake(30, 30)];
CCMenu *menu = [CCMenu menuWithItems:back,nil];
[menu setPosition:CGPointZero];
[self addChild:menu];
If that doesn't work, your back method may need to accept the parameter passed when the menu button is pressed, like this:
-(void) back:(CCMenuItem*) item;
If that's the case you'll need to add the parameter to the @selector call:
...selector:@selector(back:)];
try this.
CCMenuItemFont *back = [CCMenuItemFont itemFromString:@"back" target:self selector:@selector(back:)];
and change your back method to be..
-(void) back: (id) sender {
精彩评论