1
votes

I am making an iOS game with sprite builder. And I designed it around a board game style. I want the user to press the play button that triggers the play method. It then generates a random number that should be displayed on a label. The label and button are on the gameplay scene. The game play scene is a subclass of CCNode. The code is on the Gameplay class that is a subclass of CCNode. I have found out that the label is nil. How do I make it not nil? The code connection for my label is doc root var assigned to _randNumLabel. My gameplay code connection is assigned Gameplay. This is my log after I open the scene and click the button:

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`

Ignore the _affectLabel as it will get fixed if _randNumLabel gets fixed.

#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
1

1 Answers

1
votes

You need to declare your instance variables using curly brackets, i.e.:

@implementation Gameplay
{ 
   CCLabelTTF *_randNumLabel;
}

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

As a personal preference I would use a private property instead of an instance variable, e.g.

@interface Gameplay ()

@property (nonatomic, strong) CCLabelTTF *randNumLabel;

@end

@implementation Gameplay

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