Cocos2d如何切换场景

时间:2013-05-19 18:38:31

标签: xcode cocos2d-iphone

如何在cocos2d中切换场景?我有我的主要课程," HellowWorldLayer.h / .m" ,但我似乎无法正确切换场景。我试过了:

[[CCDirector sharedDirector] replaceScene:[Race node]];
//And
[[CCDirector sharedDirector] replaceScene:[Race Scene]];

我设置了一个断点,我可以看到它将进入新场景,但我添加到新场景的Button不会显示出来。我正确地创建了按钮。然后我将它添加到新场景中的屏幕上,如下所示:

 [self addChild:Menu];

我刚从HolloWorldLayer.m / .h获取了所有代码并将其复制到新场景(类)中,但我无法正确转换。我是xcode和cocos2d的新手,很抱歉,如果这很简单的话。

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

1 个答案:

答案 0 :(得分:1)

您的代码对我有用。

以下是示例:Download

使用转换以获得更好的效果。

[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[Race scene] ]];