tags:

views:

32

answers:

1

I am using two calls setScore which is going in the init method and UpdateScore when an object is destroyed. When I run the program, I get a crash as soon as UpdateScore is called. Anyone see errors with my code? Thank you very much.

In my .h file I have CCLabel *score; and NSString *text; declared for global use.

-(void)setScore{

 scorE = 1;

 text = [[NSString alloc] initWithFormat:@"%d", scorE];

 score = [CCLabel labelWithString:text fontName:@"Marker Felt" fontSize:18];

 score.position = ccp(45, 310);

 [self addChild: score];
}

-(void)UpdateScore{

 scorE++;

 NSLog(@"score +1");

 [score setString: [NSString stringWithFormat:@"%d",scorE]];

}
+1  A: 

It might be possible as you are using class method of CCLabel which is auto-releasing your score object. Try to use options below:

1) score = [[CCLabel labelWithString:text fontName:@"Marker Felt" fontSize:18] retain]; 2) score = [[CCLabel alloc] initWithString:text fontName:@"Marker Felt" fontSize:18];

Don't forget to release your score object in your dealloc (or wherever required).

Sagar
Thank you so much Sagar, got it (:
Brandyn