我在演示代碼中實現了三個不同的按鈕。每個按鈕都會進入同一班級的場景,但每次按鈕都會顯示不同的scenerio作爲點擊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]];
}