cocos2d loading scene failed
I'm currently reading "Learn iPhone and iPad Cocos2D Game Development". i am not able to use a loading scene. i have HelloWorldScene.m, when i click it, it should show ScoreScene.m with a LoadingScene in between. and when i click it again, should go back. but it wont happen. the update method won't be executed(i added a CCLOG() there) here is my code:
LoadingScene.m:
#import "LoadingScene.h"
#import "ScoreScene.h"
#import "HelloWorldScene.h"
@implementation LoadingScene
+(id) sceneWithTargetScene: (TargetScenes) targetScene {
return [[[self alloc] initWithTargetScene: targetScene] autorelease];
}
-(id) initWithTargetScene: (TargetScenes) targetScene {
if ((self = [super init])) {
targetScene_ = targetScene;
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Loading..." fontName:@"Marker Felt" fontSize:64];
[self addChild:label];
CCLOG(@"this is inside initwithtargetscene");
[self scheduleUpdate];
}
return self;
}
-(void) update: (ccTime) delta {
CCLOG(@"this is inside update");
[self unscheduleAllSelectors];
switch (targetScene_) {
case TargetSceneScoreScene:
[[CCDirector sharedDirector] replaceScene:[ScoreScene scene]];
break;
case TargetSceneHelloWorld:
[[CCDirector sharedDirector] replaceScene:[HelloWorld scene]];
break;
default:
break;
}
}
@end
ScoreScene.m:
#import "ScoreScene.h"
#import "LoadingScene.h"
@implementation ScoreScene
+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
ScoreScene *layer = [ScoreScene node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// 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] )) {
for (int i = 0; i < 4000000; i++) {
int b = 343 / 247;
}
self.isTouchEnabled = YES;
// create and initialize a Label
CCLabelTTF *label = [CCLabelTTF labelWithString:@"this is a score" 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];
}
return self;
}
-(void) ccTouchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
开发者_如何学Go [LoadingScene sceneWithTargetScene: TargetSceneHelloWorld];
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
@end
HelloWorldScene.m
#import "HelloWorldScene.h"
#import "LoadingScene.h"
// HelloWorld implementation
@implementation HelloWorld
+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorld *layer = [HelloWorld node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// 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] )) {
self.isTouchEnabled = YES;
// 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];
}
return self;
}
- (void) ccTouchesBegan:(NSSet*) touches withEvent: (UIEvent *) event {
CCLOG(@"this is inside touches began");
[LoadingScene sceneWithTargetScene:TargetSceneScoreScene];
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
@end
the method is never call you didn't loaded the LoadingScene in the director. Cocos2d schedules the update methods when the scene is managed by the director.
In your HelloWorldScene.m you must replace the current scene with the loading scene like that:
- (void) ccTouchesBegan:(NSSet*) touches withEvent: (UIEvent *) event {
CCLOG(@"this is inside touches began");
id loadingScene = [LoadingScene sceneWithTargetScene:0]; // Init the scene
// Replace the current scene with the given one.
[[CCDirector sharedDirector] replaceScene:loadingScene];
// The director can now manages schedulers into the loadingScene
}
精彩评论