objective-ccocos2d-iphoneselectorccmenuitem

Changing the selector of a CCMenuItem


Is it possible to reassign the selector of a CCMenuItem once it's been created?

I'm looking to do something like this:

//Initialization:
_menuItem =[CCMenuItemSprite itemWithNormalSprite:normalSprite
selectedSprite:selectedSprite target:self selector:@selector(nextPage:)];

//Later
_menuItem.selector = @selector(mainMenu:);

Is this possible? I couldn't find anything about it in the CCMenuItem documentation. I admit I'm not all that clear on how selectors actually work in obj-c.


Solution

  • Yes, you can use -setTarget:selector:

    [item setTarget:self selector:@selector(mainMenu:)];
    

    It is in the documentation, http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_menu_item.html#a58d8f2c72697ea34cf1e10b82cac81ff