Cocos2d How to Switch Scenes

十年热恋 提交于 2019-12-19 10:16:38

问题


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]];

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];

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

回答1:


Your code as it is worked for me.

Here is Sample: Download

Use transition for better look.

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


来源:https://stackoverflow.com/questions/16638128/cocos2d-how-to-switch-scenes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!