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.

有帮助吗?

解决方案

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

其他提示

Not MainScene, just self.

self.CakesEaten = 9999
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top