Ios 带spritebuilder的零标签,can';我想不出怎么修理它

Ios 带spritebuilder的零标签,can';我想不出怎么修理它,ios,objective-c,spritebuilder,Ios,Objective C,Spritebuilder,我正在用sprite builder制作一个iOS游戏。我围绕棋盘游戏风格设计了它。我希望用户按下触发play方法的play按钮。然后,它生成一个随机数,该随机数应显示在标签上。标签和按钮在游戏场景中。游戏场景是CCNode的一个子类。代码位于Gameplay类上,该类是CCNode的子类。我发现标签是零。我如何使它不为零?我的标签的代码连接是分配给\u randNumLabel的文档根变量。我的游戏代码连接已分配gameplay。这是我打开场景并单击按钮后的日志: 2014-06-09 17:

我正在用sprite builder制作一个iOS游戏。我围绕棋盘游戏风格设计了它。我希望用户按下触发
play
方法的play按钮。然后,它生成一个随机数,该随机数应显示在标签上。标签和按钮在游戏场景中。游戏场景是
CCNode
的一个子类。代码位于
Gameplay
类上,该类是
CCNode
的子类。我发现标签是零。我如何使它不为零?我的标签的代码连接是分配给
\u randNumLabel
的文档根变量。我的游戏代码连接已分配
gameplay
。这是我打开场景并单击按钮后的日志:

2014-06-09 17:20:12.565 Sunk[6037:60b] CCBReader: Couldn't find member variable: _randNumLabel
2014-06-09 17:20:12.567 Sunk[6037:60b] CCBReader: Couldn't find member variable: _affectLabel
2014-06-09 17:20:19.513 Sunk[6037:60b] Nil`
忽略
\u affectLabel
,因为如果
\u randNumLabel
得到修复,它将得到修复

#import "Gameplay.h"

@implementation Gameplay
CCLabelTTF *_randNumLabel;
- (void)play {
    if (_randNumLabel == nil)
    {
        CCLOG(@"Nil");
    }
    if (_randNumLabel !=nil)
    {
        CCLOG(@"play button pressed!");
        int max = 6;
        int randNumber = (arc4random() % max) + 1; // Generates a number between 1-6.
        CCLOG(@"Random Number %d", randNumber);
        _randNumLabel.string = [NSString stringWithFormat:@"Number: %d", randNumber];
    }
}
- (void)update:(CCTime)delta {

}

@end

您需要使用花括号声明实例变量,即:

@implementation Gameplay
{ 
   CCLabelTTF *_randNumLabel;
}

- (void)play {
// rest of your code
...
作为个人偏好,我会使用私有属性而不是实例变量,例如

@interface Gameplay ()

@property (nonatomic, strong) CCLabelTTF *randNumLabel;

@end

@implementation Gameplay

- (void)play {
// rest of your code
...