1. new 一个 cocos2d项目
2. 添加一个场景类
// .h文件 #import "CCScene.h" @interface CCScene_another : CCScene @end // .m文件 #import "CCScene_another.h" @implementation CCScene_another - (id)init{ if (self = [super init]) { NSLog(@"CCScene_another__init__"); } return self; } - (void)onEnter{ [super onEnter]; NSLog(@"CCScene_another__onEnter__"); } - (void)onEnterTransitionDidFinish{ [super onEnterTransitionDidFinish]; NSLog(@"CCScene_another__onEnterTransitionDidFinish__"); } - (void)onExit{ [super onExit]; NSLog(@"CCScene_another__onExit__"); } @end
3. 在HelloWorldLayer.m的init中添加一个replaceScene,当然要包含头文件"CCScene_another.h"
#import "CCScene_another.h"
- (void)change_scene{ [[CCDirector sharedDirector] replaceScene:[CCScene_another node]]; } // on "init" you need to initialize your instance -(id) init { // always call "super" init // Apple recommends to re-assign "self" with the "super" return value if( (self=[super init])) { NSLog(@"hello__init__"); // create and initialize a Label CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64]; // ask director the the window size CGSize size = [[CCDirector sharedDirector] winSize]; // position the label on the center of the screen label.position = ccp( size.width /2 , size.height/2 ); // add the label as a child to this Layer [self addChild: label]; // replaceScene [self performSelector:@selector(change_scene) withObject:nil afterDelay:0.1f]; } return self; }
4. 输出信息
5.结论(下图非自画)