Creating Menu in Iphone Cocos2D or Box2D?

By | April 3, 2012

Use CCMenuItemSprite and CCMenu in iPhone to create menu.

-(void) setUpMenu
{
	 CCSprite *Home1 = [CCSprite spriteWithFile:@"Home1.png"];
	 CCSprite *Home2 = [CCSprite spriteWithFile:@"Home1.png"];
	 Home2.opacity = 100;

	 CCSprite *Levels1 = [CCSprite spriteWithFile:@"levels2.png"];
	 CCSprite *Levels2 = [CCSprite spriteWithFile:@"levels2.png"];
	 Levels2.opacity = 100;

	 CCSprite *Refresh1 = [CCSprite spriteWithFile:@"refresh.png"];
	 CCSprite *Refresh2 = [CCSprite spriteWithFile:@"refresh.png"];
	 Refresh2.opacity = 100;

	 CCSprite *go_back1 = [CCSprite spriteWithFile:@"back2.png"];
	 CCSprite *go_back2 = [CCSprite spriteWithFile:@"back2.png"];
	 go_back2.opacity = 100;

	 CCMenuItemSprite  *top_menuSprite1 = [CCMenuItemSprite itemFromNormalSprite:Home1 selectedSprite:Home2 target:self selector:@selector(goHome)];
	 CCMenuItemSprite  *top_menuSprite2 = [CCMenuItemSprite itemFromNormalSprite:Levels1 selectedSprite:Levels2 target:self selector:@selector(goToLevelSelection)];
	 CCMenuItemSprite  *top_menuSprite3 = [CCMenuItemSprite itemFromNormalSprite:Refresh1 selectedSprite:Refresh2 target:self selector:@selector(reloadGame)];
	 CCMenuItemSprite  *top_menuSprite4 = [CCMenuItemSprite itemFromNormalSprite:go_back1 selectedSprite:go_back2 target:self selector:@selector(menuGoBack)];
	 top_menu = [CCMenu menuWithItems:top_menuSprite1,top_menuSprite2, top_menuSprite3 ,top_menuSprite4, nil];
	 [top_menu  alignItemsVerticallyWithPadding:10.0f];
	 top_menu.position = ccp(240,160);

	 [self addChild:top_menu z:2];
}

call this function to set up the menu for your home page in your iPhone game or you can change this code directly to ANDROID and it will work.
Make sure you have all the images mentioned in the above example.
This menu will appear on the centre of the screen in Landscape mode.

Leave a Reply

Your email address will not be published. Required fields are marked *