0

我正在练习制作一个使用场景控制器(状态管理器)类切换场景的简单应用程序。

我创建了我的场景:

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];

    GameMenu *layer = [GameMenu node];

    [scene addChild: layer];

    return scene;
}

-(id)init{
    if ((self = [super init])){
        self.isTouchEnabled = YES;
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        gameMenuLabel = [CCLabelTTF labelWithString:@"This is the Main Menu. Click to Continue" fontName:@"Arial" fontSize:13];
        gameMenuLabel.position = ccp(winSize.width/2, winSize.height/1.5);
        [self addChild:gameMenuLabel];
    }

    return  self;
}

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"ccTouchesBegan: called from MainMenu object");
    [[StateManager sharedStateManager] runSceneWithID:kGamePlay];

}

-(void)dealloc{
    [gameMenuLabel release];
    gameMenuLabel = nil;

    [super dealloc];
}

@end

但我不断收到此警告:https ://dl.dropbox.com/u/1885149/Screen%20Shot%202012-10-06%20at%205.23.43%20PM.png (可能帮助不大,但我想我会的链接截图。

我认为这与dealloc有关。如果我在场景中注释掉 dealloc,我不会收到此警告。任何帮助将不胜感激,谢谢。

这是我的statemanager切换场景的方法:

-(void)runSceneWithID:(SceneTypes)sceneID {
    SceneTypes oldScene = currentScene;
    currentScene = sceneID;
    id sceneToRun = nil;
    switch (sceneID) {
        case kSplashScene:
            sceneToRun = [SplashScene node];
            break;

        case kGameMenu:
            sceneToRun = [GameMenu node];
            break;
        case kGamePlay:
            sceneToRun = [GamePlay node];
            break;
        case kGameOver:
            sceneToRun = [GameOver node];
            break;

        default:
            CCLOG(@"Unknown ID, cannot switch scenes");
            return;
            break;
    }
    if (sceneToRun == nil) {
        // Revert back, since no new scene was found
        currentScene = oldScene;
        return;
    }    
    if ([[CCDirector sharedDirector] runningScene] == nil) {
        [[CCDirector sharedDirector] runWithScene:sceneToRun];
    } else {
        [[CCDirector sharedDirector] replaceScene:sceneToRun];
    }
}
4

1 回答 1

1

你应该像这样给 gameMenuLabel 一个保留属性

@property (nonatomic, retain) CCLabelTTF* gameMenuLabel; //in .h file

并写下这个......

    self.gameMenuLabel = [CCLabelTTF labelWithString:@"This is the Main Menu. Click to Continue" fontName:@"Arial" fontSize:13];

而不是这个...

    gameMenuLabel = [CCLabelTTF labelWithString:@"This is the Main Menu. Click to Continue" fontName:@"Arial" fontSize:13];

问题是你给 gameMenuLabel 一个自动释放的对象,然后在 dealloc 部分再次释放该对象。因此,崩溃。

于 2012-10-07T07:01:11.823 回答