#import "HelloWorldLayer.h"
#import "Player.h"
#import "MyObject.h"
//#import "SettingsManager.h"
// HelloWorldLayer implementation
@implementation HelloWorldLayer
@synthesize myObject,newPlayer;
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
usrDef = [NSUserDefaults standardUserDefaults];
//CGSize
CGSize winSize = [[CCDirector sharedDirector]winSize];
//settingsmangaer
//myValue = [[SettingsManager sharedSettingsManager] getInt:@"hiscore"];
//child
newPlayer = [[Player alloc]init];
[self addChild:newPlayer];
CCMenuItem *myMenuItem = [CCMenuItemImage itemFromNormalImage:@"Button0000.png" selectedImage:@"Button0001.png" target:self selector:@selector(buttonPressed:) ];
CCMenu *myMenu = [CCMenu menuWithItems:myMenuItem, nil];
myMenu.position = ccp(50, 50);
[self addChild:myMenu];
//cclabel
myValue = [usrDef integerForKey:@"difficulty"];
myLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Hello: %d",myValue] fontName:@"Arial" fontSize:50];
[myLabel setColor:ccBLACK];
// [myLabel setCString:@"hihi: %d",myValue];
myLabel.position = ccp(winSize.width/2,winSize.height/2);
[self addChild:myLabel];
}
return self;
}
I have a buttton here to add 1 to myValue when pressed and store it to nsdefaults.
-(void)buttonPressed:(id)sender{
myValue +=1;
[myLabel setString:[NSString stringWithFormat:@"Hihi: %d",myValue]];
[usrDef setInteger:myValue forKey:@"diffculty"];
[usrDef synchronize];
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
@end
The nsdefault i’m using is not working. When i reopen the application the label starts from 0 again. The program compiles properly and i’m not getting any error. Please help thanks.
Above is the proper syntax for what you want to do
Below is how to retrieve the data.