I have a Menu interface that inherits from CCLayer, with two functions: init (overriden from CCLayer) and CreateMenuItem (defined in Menu). When I create a CCMenuItem and add it as a child of CCMenu in the init function, everything works just right. But when I do the same (create a CCMenuItem and add it as a child of CCMenu) in the CreateMenuItem function, the item does not display on the screen. It only works if I call CreateMenuItem from within init, but not if I call it from anywhere else, so it appears that the only way of getting it to work is creating and adding the item within the init function. By the way, this is based on the MenuTest example that comes with Cocos.
This is my code:
@interface Menu : CCLayer
{
NSMutableDictionary *itemPool;
CCMenu *menu;
}
- (void) CreateMenuItemWithName:(NSString*)name image:(NSString*)staticImage rollover:(NSString*)rolloverImage selector:(NSString*)function;
@end
@implementation Menu
- (id) init
{
if ((self = [super init]))
{
windowSize = [[CCDirector sharedDirector] winSize];
itemPool = [[NSMutableDictionary alloc] init];
CCMenuItem *gift = [CCMenuItemImage itemFromNormalImage:@"UI_button_done01_small_static.png" selectedImage:@"UI_button_done01_small_rollover.png" target:self selector:@selector(giftCallback:)];
[gift setAnchorPoint:ccp(1, 0)];
gift.position = ccp(windowSize.width/2 - 55, -windowSize.height/2);
menu = [CCMenu menuWithItems:nil];
[menu addChild:gift z:1 tag:1]; ---> Everything's fine, the item is displayed on the screen
[self addChild:menu];
}
return self;
}
- (void) CreateMenuItemWithName:(NSString*)name image:(NSString*)staticImage rollover:(NSString*)rolloverImage selector:(NSString*)function
{
CCMenuItem *item = [CCMenuItemImage itemFromNormalImage:staticImage selectedImage:rolloverImage target:self selector:NSSelectorFromString(function)];
[item setAnchorPoint:ccp(0, 0)];
item.position = ccp(windowSize.width/2 - 110, -windowSize.height/2);
[itemPool setObject:item forKey:name];
[menu addChild:item z:1 tag:1]; ---> Although I added the child, it is not displayed on the screen
}
Has anyone had this problem, or knows a way to fix it?
Thanks!