【问题标题】:Cocos2d CCLabel update problem for scoresCocos2d CCLabel 更新分数问题
【发布时间】:2010-10-28 01:35:08
【问题描述】:

我正在使用两个调用 setScore,它们在 init 方法中进行,而在对象被销毁时使用 UpdateScore。当我运行程序时,只要调用 UpdateScore,我就会崩溃。有人看到我的代码有错误吗?非常感谢。

在我的 .h 文件中,我有 CCLabel *score;和 NSString *文本;声明供全球使用。

-(void)setScore{

 scorE = 1;

 text = [[NSString alloc] initWithFormat:@"%d", scorE];

 score = [CCLabel labelWithString:text fontName:@"Marker Felt" fontSize:18];

 score.position = ccp(45, 310);

 [self addChild: score];
}

-(void)UpdateScore{

 scorE++;

 NSLog(@"score +1");

 [score setString: [NSString stringWithFormat:@"%d",scorE]];

}

【问题讨论】:

  • 崩溃时的错误信息是什么?
  • 节目接收信号:“EXC_BAD_ACCESS”。

标签: iphone cocos2d-iphone


【解决方案1】:

这可能是因为您正在使用 CCLabel 的类方法,它会自动释放您的分数对象。尝试使用以下选项:

1) score = [[CCLabel labelWithString:text fontName:@"Marker Felt" fontSize:18] retain]; 2)score = [[CCLabel alloc] initWithString:text fontName:@"Marker Felt" fontSize:18];

不要忘记在你的 dealloc(或任何需要的地方)中释放你的 score 对象。

【讨论】:

  • 非常感谢 Sagar,知道了 (:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多