2012-10-06 58 views
0

我正在练习制作一个简单的应用程序,用场景控制器(状态管理器)类来切换场景。Dealloc给出了警告

我创造了我的场景:

+(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,我没有得到这个警告。任何帮助将不胜感激,谢谢

这是我的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]; 
    } 
} 
+1

您应该以文本格式将警告添加到您的帖子,以便它变得可搜索。 –

+0

截图中的代码与您问题中的代码完全不同。 – jrturton

+0

您的屏幕截图显示了断点或崩溃,而不是“警告” – newacct

回答

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节中。因此,崩溃。

+0

如果可能,切换到ARC。 Xcode甚至可以为你做这个。看下编辑>重构 – nielsbot

+0

谢谢你摇滚! – HelloWorld

+0

如果你喜欢答案,你可以随时接受它! :) – mayuur

相关问题