我正在阅读“学习iPhone和iPad Cocos2D游戏开发”。我无法使用加载场景。我有HelloWorldScene.m,当我点击它时,它应该显示ScoreScene.m,其间有一个LoadingScene。当我再次点击它时,应该回去。但它不会发生。更新方法将不会执行(我添加了一个CCLOG())这里是我的代码:
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 {
[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
答案 0 :(得分:1)
该方法永远不会调用您没有在Director中加载LoadingScene。当导演管理场景时,Cocos2d会安排更新方法。
在HelloWorldScene.m中,您必须将当前场景替换为加载场景,如下所示:
- (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
}