Cocos2d如何切换场景 [英] Cocos2d How to Switch Scenes
问题描述
如何切换cocos2d中的场景?我有我的主类,HellowWorldLayer.h / .m,但我似乎不能正确切换场景。我试过:
How do i switch scenes in cocos2d? I have my main class, "HellowWorldLayer.h/.m" , but i can't seem to switch scenes correctly. I have tried :
[[CCDirector sharedDirector] replaceScene:[Race node]];
//And
[[CCDirector sharedDirector] replaceScene:[Race Scene]];
我放一个断点,我可以看到它是去新的场景,但Button i添加到新场景不显示。我正确创建按钮。然后我把它添加到新的场景中的屏幕像这样:
I put a break point in and i can see it is going to the new scene but the Button i added to the new scene isn't showing up. I am creating the button correctly. Then i am adding it to the screen in the new scene like this:
[self addChild:Menu];
我刚刚从HolloWorldLayer.m / .h中取得了所有代码,并将其复制到新场景(类),但我无法正确转换。我是新到xcode和cocos2d所以很抱歉,如果这是真的很简单。
I just took all of the code from HolloWorldLayer.m/.h and copied it into the new scene (class) but i am unable to transition correctly. I am new to xcode and cocos2d so sorry if this is really simple.
Race.m
#import "Race.h"
#import "Intermediary.h"
@implementation Race
+(CCScene *) scene{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
Race *layer = [Race node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init{
if( (self=[super init]) ) {
NSLog(@"Racing!");
CCLabelTTF *startLbl = [CCLabelTTF labelWithString:@"Start" fontName:@"Marker Felt" fontSize:20];
CCMenuItemLabel *startMenu = [CCMenuItemLabel itemWithLabel:startLbl block:^(id sender) {
NSLog(@"I have been pushed!");
}];
startMenu.position = ccp(50,50);
CCMenu *Menu = [CCMenu menuWithItems:startMenu, nil];
[self addChild:Menu];
}
return self;
}
@end
推荐答案
你的代码,因为它是为我工作。
Your code as it is worked for me.
以下是范例:下载
使用转换效果更好。
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[Race scene] ]];
这篇关于Cocos2d如何切换场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!