Question

I'm using Cocos2d and looking to access a variable (CakesEaten) from another CCScene.

// in MainScene.h
@interface MainScene : CCScene{
    int CakesEaten;
}
@property int CakesEaten;

// in MainScene.m
@implementation
@synthesize CakesEaten;

// at the top of ScoreScreen.m
#import "MainScene.h"
// in the ScoreScreen.m init method
MainScene.CakesEaten = 9999

Gives an error of: Property "CakesEaten" not found on object of type MainScene. It's the only error that I receive when building.

Was it helpful?

Solution

In order to access any property, synthesized or not, you need an instance of the class:

MainScene *scene = [[MainScene alloc] init];
scene.CakesEaten = 9999;

Note: the latest editions of Xcode do not require @synthesize, unless you would like to change the name for the variable that is used by default. You can safely remove that line, along with the declaration of the member variable.

The end result should look like this:

@interface MainScene : CCScene
@property (nonatomic, readwrite) int CakesEaten;
@end

OTHER TIPS

Not MainScene, just self.

self.CakesEaten = 9999
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top