While developing games you need to create Menu. Cocos2d's CCMenu makes your life easier.
You can have your own custom fancy image text indicating menu items using CCMenu.
Features and Limitation:
- You can add MenuItem objects in runtime using addChild:
- But the only accepted children are MenuItem objects
You can get class reference of CCMenu at http://www.cocos2d-iphone.org/api-ref/0.99.3/interface_c_c_menu.html
//Creating the three menu items.
//You can use different images for selected view & normal view.
//On clicking menu will show selectedImage
CCMenuItemImage *run = [CCMenuItemImage
itemFromNormalImage:@"run.png"
selectedImage:@"run-selected.png"
target:self
selector:@selector(runAction:)];
CCMenuItemImage *fire = [CCMenuItemImage
itemFromNormalImage:@"fire.png"
selectedImage:@"fire-selected.png"
target:self
selector:@selector(fireAction:)];
CCMenuItemImage *jump = [CCMenuItemImage
itemFromNormalImage:@"jump.png"
selectedImage:@"jump-selected.png"
target:self
selector:@selector(jumpAction:)];
//Adding menu items to the CCMenu. Don't forget to include 'nil'
CCMenu *selectMenu= [CCMenu menuWithItems:run, fire, jump, nil];
//Aligning & Adding CCMenu child to the scene
[selectMenu alignItemsVertically];
[self addChild:selectMenu];
//Different functions on below get called according to the menu item clicked.
- (void)runAction:(id)sender
{
[[CCDirector sharedDirector] replaceScene:[runningScene node]];
}
- (void)fireAction:(id)sender
{
[[Director sharedDirector] replaceScene:[fireScene node]];
}
- (void)jumpAction:(id)sender
{
[[Director sharedDirector] replaceScene:[jumpingScene node]];
}
No comments:
Post a Comment