Bob
Bob

Reputation: 1

Cocos2D CCMenuItem over an other CCLayer doesn't respond

Here is my 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

Upvotes: 0

Views: 515

Answers (2)

Ian Hatch
Ian Hatch

Reputation: 1371

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:)];

Upvotes: 1

Bongeh
Bongeh

Reputation: 2300

try this.

CCMenuItemFont *back = [CCMenuItemFont itemFromString:@"back" target:self selector:@selector(back:)];

and change your back method to be..

-(void) back: (id) sender {

Upvotes: 0

Related Questions