
You can create a menu by creating label items or image items and passing them to a CCMenu object. A typical MainMenuScene.m file might look like this:
-(void)playScene:(CCMenuItemFont*)itemPassedIn {
if ([itemPassedIn tag] == 1) {
[[GameManager sharedGameManager] runSceneWithID:kIntroScene];
} else if ([itemPassedIn tag] == 2) {
[[GameManager sharedGameManager] runSceneWithID:kSecondScene];
} else if ([itemPassedIn tag] == 3) {
[[GameManager sharedGameManager] runSceneWithID:kThirdScene];
} else {
CCLOG(@”Unexpected item. Tag was: %d”, [itemPassedIn tag]);
}
}
-(void)displayMainMenu {
CGSize screenSize = [CCDirector sharedDirector].winSize;
if (sceneSelectMenu != nil) {
[sceneSelectMenu removeFromParentAndCleanup:YES];
}
// Main Menu
CCMenuItemImage *playGameButton = [CCMenuItemImage
itemWithNormalImage:@”TapMeToPlay.png”
selectedImage:@”TapMeToPlay.png”
disabledImage:nil
target:self
selector:@selector(displaySceneSelection)];
CCMenuItemImage *optionsButton = [CCMenuItemImage
itemFromNormalImage:@”someImage.png”
selectedImage:@”someImage.png”
disabledImage:nil
target:self
selector:@selector(showOptions)];
mainMenu = [CCMenu menuWithItems:playGameButton,optionsButton,nil];
[mainMenu alignItemsVerticallyWithPadding:screenSize.height * 0.059f];
[mainMenu setPosition:ccp(screenSize.width * 2,screenSize.height / 2)];
id moveAction =
[CCMoveTo actionWithDuration:1.2f
position:ccp(screenSize.width * 0.75f,
screenSize.height * 0.70f)];
id moveEffect = [CCEaseIn actionWithAction:moveAction rate:1.0f];
[mainMenu runAction:moveEffect];
[self addChild:mainMenu z:0 tag:kMainMenuTagValue];
}
-(void)displaySceneSelection {
CGSize screenSize = [CCDirector sharedDirector].winSize;
if (mainMenu != nil) {
[mainMenu removeFromParentAndCleanup:YES];
}
CCLabelTTF *playScene1Label = [CCLabelTTF labelWithString:@”Level 1″ fontName:@”Motter Tektura” fontSize:45];
playScene1Label.color=ccc3(000, 000, 000);
CCMenuItemLabel *playScene1 = [CCMenuItemLabel itemWithLabel:playScene1Label target:self selector:@selector(playScene:)];
[playScene1 setTag:1];
[playScene1 addChild:stroke z:-1];
CCLabelTTF *playScene2Label = [CCLabelTTF labelWithString:@”Level 2″ fontName:@”Motter Tektura” fontSize:45];
playScene2Label.color=ccc3(000, 000, 000);
CCMenuItemLabel *playScene2 = [CCMenuItemLabel itemWithLabel:playScene2Label target:self selector:@selector(playScene:)];
[playScene2 setTag:2];
[playScene2 addChild:stroke2 z:-1];
CCLabelTTF *playScene3Label = [CCLabelTTF labelWithString:@”Level 3″ fontName:@”Motter Tektura” fontSize:45];
playScene3Label.color=ccc3(000, 000, 000);
CCMenuItemLabel *playScene3 = [CCMenuItemLabel itemWithLabel:playScene3Label target:self selector:@selector(playScene:)];
[playScene3 setTag:3];
[playScene3 addChild:stroke3 z:-1];
CCLabelBMFont *backButtonLabel =
[CCLabelBMFont labelWithString:@”Back”
fntFile:@”MyFontFile.fnt”];
CCMenuItemLabel *backButton =
[CCMenuItemLabel itemWithLabel:backButtonLabel target:self
selector:@selector(displayMainMenu)];
sceneSelectMenu = [CCMenu menuWithItems:playScene1,
playScene2,playScene3,backButton,nil];
[sceneSelectMenu alignItemsVerticallyWithPadding:screenSize.height * 0.059f];
[sceneSelectMenu setPosition:ccp(screenSize.width * 2,screenSize.height / 2)];
id moveAction = [CCMoveTo actionWithDuration:0.5f position:ccp(screenSize.width * 0.5f,screenSize.height/2)];
id moveEffect = [CCEaseIn actionWithAction:moveAction rate:1.0f];
[sceneSelectMenu runAction:moveEffect];
[self addChild:sceneSelectMenu z:1 tag:kSceneMenuTagValue];
}
There are three methods here that are important:
1) playScene which basically receives an item and based on the received item it calls CCDirector to play a scene.
2) displayMainMenu which uses images as buttons to call a second level menu item. It basically creates menu items and then uses them to populate CCMenu menuWithItems.
3) displaySceneSelection which is called from the displayMainMenu which then takes the user selection and passes it to playScene.