Cocos2D CCMenu – An Example
Hi,
While using cocos2D you may need to create some scenes with menu items on it. This example given below shows , how to create a cocos2D menu scene with menu items using images. You can have your own custom fancy image text indicating menu items using this.
//Creating the three menu items.
//You can use different images for selected view & normal view.
//On clicking menu will show selectedImage
CCMenuItemImage *Item1 = [CCMenuItemImage
itemFromNormalImage:@"Walk.png"
selectedImage:@"WalkTwo.png"
target:self
selector:@selector(walkAction:)];
CCMenuItemImage *Item2 = [CCMenuItemImage
itemFromNormalImage:@"Run.png"
selectedImage:@"RunTwo.png"
target:self
selector:@selector(runAction:)];
CCMenuItemImage *Item3 = [CCMenuItemImage
itemFromNormalImage:@"Jump.png"
selectedImage:@"JumpTwo.png"
target:self
selector:@selector(jumpAction:)];
//Adding menu items to the CCMenu. Don't forget to include 'nil'
CCMenu *selectMenu= [CCMenu menuWithItems:Item1, Item2, Item3, 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)walkAction:(id)sender
{
[[CCDirector sharedDirector] replaceScene:[WalkingScene node]];
}
- (void)runAction:(id)sender
{
[[Director sharedDirector] replaceScene:[runningScene node]];
}
- (void)jumpAction:(id)sender
{
[[Director sharedDirector] replaceScene:[jumpingScene node]];
}
Link to this post!