跳转到Cocos2d iphone游戏的主屏幕

时间:2013-06-22 06:15:02

标签: cocos2d-iphone

几个月前我已经开始在cocos2d iphone上开发了。现在我即将结束我的比赛,我停留在一个位置。我知道这是编码问题。

首先我要解释主屏幕。当游戏开始时,有一些动画需要大约7秒,然后我的播放按钮出现。然后我点击它并开始游戏。

现在我想实现一个按钮,当我点击那个按钮时,场景将是过境,然后直接我可以看到我的主菜单没有动画。

所以基本上我不希望用户先等待动画完成。我希望他们直接玩游戏。

任何指南或提示都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我在演示代码上实现了我放三个不同的按钮。每个按钮都会进入同一个类的场景,但每次点击par按钮时都会显示不同的场景。

这是 HelloWorldLayer.h

#import "cocos2d.h"

@interface HelloWorldLayer : CCLayer
{
}

// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene:(int)level;
+(id)nodeWithGameLevel:(int)level;
-(id) initWithGameLevel:(int)level;
@end

HelloWorldLayer.m

@implementation HelloWorldLayer
+(CCScene *) scene:(int)whichLevel
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer nodeWithGameLevel:whichLevel];
    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}
-(id) initWithGameLevel:(int)level
{
    if( (self=[super init]) ) {
        printf("level:%d",level);
        if (level == 1) {
            //Code for scene with full animation.
        }else if(level == 2){
            //code for scene without animation.
        }else if(level == 3){
            // any other scene which you want.
        }
        CCMenuItemImage *item1 = [CCMenuItemImage itemWithNormalImage:@"Icon-72.png" selectedImage:@"Icon-72.png" target:self selector:@selector(goToMainMenu)];
        CCMenuItemImage *item2 = [CCMenuItemImage itemWithNormalImage:@"Icon-72.png" selectedImage:@"Icon-72.png" target:self selector:@selector(goToRestartMenu)];
        CCMenuItemImage *item3 = [CCMenuItemImage itemWithNormalImage:@"Icon-72.png" selectedImage:@"Icon-72.png" target:self selector:@selector(goToMainPage)];
        CCMenu *menu = [CCMenu menuWithItems:item1,item2,item3, nil];
        [self addChild:menu];
        [menu alignItemsVertically];
    }
    return self;
}

+(id)nodeWithGameLevel:(int)level{
    return  [[[self alloc] initWithGameLevel:level] autorelease];
}
-(void)goToMainMenu{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[HelloWorldLayer scene:1] withColor:ccWHITE]];
}
-(void)goToRestartMenu{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[HelloWorldLayer scene:2] withColor:ccWHITE]];
}
-(void)goToMainPage{
    [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[HelloWorldLayer scene:3] withColor:ccWHITE]];
}