Case Wright
Case Wright

Reputation: 21

how do I add a menu to an already-made game in cocos2d and box2d for iphone?

I have an existing game that I want to have a title screen with a button "play" to the app that loads the game. EDIT: The interface was made in Level Helper

Upvotes: 0

Views: 510

Answers (3)

user1163722
user1163722

Reputation:

Here's how you can implement a menu that changes scenes with a transition. In your HelloWorldLayer.m file, add this:

-(id) init
{
if( (self=[super init])) {

    CCMenuItemImage *menuImage = [CCMenuItemImage itemFromNormalImage:@"yourimage.png" selectedImage:@"Icon.png" target:self selector:@selector(changeScene:)];

    CCMenu *menu;

    menu = [CCMenu menuWithItems:menuImage, nil];

    [self addChild:menu];
}
return self;
}

-(void) changeScene:(id)sender
{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionZoomFlipX transitionWithDuration:1 scene:[Scene1 node]]];
}

This creates a menu item image assigned to a selector, adds it to a menu, and then on click, transitions to a new scene, which I will show you how to do now. Create a new class called Scene1, and just to show that the transition worked, we will add a sprite in this new scene. In your init method:

-(id) init
{
if( (self=[super init])) {

    sprite = [CCSprite spriteWithFile:@"yourimage.png"];

    sprite.position = ccp(100,200);
    [self addChild:sprite];
}
return self;
}

If you see this new sprite on the screen, it means that everything worked.

Upvotes: 1

mattblessed
mattblessed

Reputation: 801

create a menu using the following code:

// Intalize your menu item
CCMenuItem *menuItem = [CCMenuItemFont itemFromString:@"This is what you want your item to say" target:self selector:@selector(selectorToHandleYourSelection)];
// Define where you want your item to be
menuItem.position = ccp(100,100);
// Intalize a menu for your menu item
CCMenu *menu = [CCMenu menuWithItems:menuItem, nil];
// Add the 'menu' as a child to your layer
[self addChild:menu];
// If the item position isn't defined then you can align the items horizontally
[menu alignItemsHorizontally];

Upvotes: 0

Ehab Amer
Ehab Amer

Reputation: 392

I'll assume by saying u have an existing game, that u have the source code of the game.

All u need is to make a new CCLayer with a CCMenu (containing the CCMenuButton u want) that all it does is load the CCLayer that loads as u start the game, and make ur CCLayer the one that gets loaded as u start the app.

Upvotes: 0

Related Questions