I have two scenes. Scene A is the game scene, where the level variable of type int changes. In class B I want to get the variable.
What I’ve got now is the following :
——————————— LevelDone.m ———————————
HelloWorldLayer *object = [[HelloWorldLayer alloc]init];
int pointsForZeroStars = [[requiredPoints objectAtIndex:0] integerValue];
int pointsForOneStar = [[requiredPoints objectAtIndex:1] integerValue];
int pointsForTwoStars = [[requiredPoints objectAtIndex:2] integerValue];
int pointsForThreeStars = [[requiredPoints objectAtIndex:3] integerValue];
NSLog(@"HEALTH = %d",object.health);
CCSprite *levelDoneWindow;
if ( object.health < pointsForZeroStars){
NSLog(@"should be 0 stars");
levelDoneWindow = [CCSprite spriteWithFile:@"leveldonescreen0stars.png"];
}
NSLog returns the initial value of health which is 100, when actually it is something smaller than 100.
In the game class I call pushscene in order to call the LevelDone scene:
—————————– HelloWorldLayer.m (GAME SCENE) ————————————-
else{
[[CCDirector sharedDirector] pushScene:[LevelDone node]];
[self generateLevelFromPlist:level];
}
The -generateFromPlist method resets the health to 100. But it is certainly called after the NSLog method in my LevelDone.m class, which nonetheless prints out that health is 100.
How do I retrieve the value of this variable properly ?
Use a singleton to manage global values.
http://getsetgames.com/2009/08/30/the-objective-c-singleton/
Basically, you can create properties in that singleton class, and it will “persist” throughout the game no matter what scene you are in etc.