我如何添加菜单到已经制作的游戏在cocos2d和box2d为iphone? [英] how do I add a menu to an already-made game in cocos2d and box2d for iphone?
问题描述
我有一个现有的游戏,我想有一个标题屏幕,一个按钮播放加载游戏的应用程序。
EDIT:界面是在Level Helper中创建的
I have an existing game that I want to have a title screen with a button "play" to the app that loads the game. The interface was made in Level Helper
推荐答案
这里是如何实现一个菜单,过渡。在 HelloWorldLayer.m
文件中,添加:
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]]];
}
这将创建一个分配给选择器的菜单项图像,将其添加到菜单,然后点击,过渡到一个新的场景,我会告诉你如何做现在。创建一个名为 Scene1
的新类,只是为了显示转换工作,我们将在这个新场景中添加一个sprite。在 init
方法中:
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;
}
如果你在屏幕上看到这个新的sprite,
If you see this new sprite on the screen, it means that everything worked.
这篇关于我如何添加菜单到已经制作的游戏在cocos2d和box2d为iphone?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!