The score is not resetting right at all,I am trying to make a high score counter where every time you passed previous high score it will update.However, right now it is resetting during the game. For example if I had high score of 2 during the game it will take 3 points just to put it up to 3 as high score instead of keep going up until it is game over. I have came to the conclusion that I need to reset it in gameoverlayer so it won't reset during game. I have been trying to to do this but no luck.
hello world
./h
#import "cocos2d.h"
// HelloWorldLayer
@interface HelloWorldLayer : CCLayer
{
int _score;
int _oldScore;
CCLabelTTF *_scoreLabel;
}
@property (nonatomic, assign) CCLabelTTF *scoreLabel;
hello world
init ./m
_score = [[NSUserDefaults standardUserDefaults] integerForKey:@"score"];
_oldScore = -1;
self.scoreLabel = [CCLabelTTF labelWithString:@"" dimensions:CGSizeMake(100, 50) alignment:UITextAlignmentRight fontName:@"Marker Felt" fontSize:32];
_scoreLabel.position = ccp(winSize.width - _scoreLabel.contentSize.width, _scoreLabel.contentSize.height);
_scoreLabel.color = ccc3(255,0,0);
[self addChild:_scoreLabel z:1];
hello world implement ./m
- (void)update:(ccTime)dt {
NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
CGRect projectileRect = CGRectMake(
projectile.position.x - (projectile.contentSize.width/2),
projectile.position.y - (projectile.contentSize.height/2),
projectile.contentSize.width,
projectile.contentSize.height);
BOOL monsterHit = FALSE;
NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
for (CCSprite *target in _targets) {
CGRect targetRect = CGRectMake(
target.position.x - (target.contentSize.width/2),
target.position.y - (target.contentSize.height/2),
target.contentSize.width,
target.contentSize.height);
if (CGRectIntersectsRect(projectileRect, targetRect)) {
CCParticleFire* explosion = [[CCParticleFire alloc] initWithTotalParticles:200];
explosion.texture =[[CCTextureCache sharedTextureCache] addImage:@"sun.png"];
explosion.autoRemoveOnFinish = YES;
explosion.startSize = 20.0f;
explosion.speed = 70.0f;
explosion.anchorPoint = ccp(0.5f,0.5f);
explosion.position = target.position;
explosion.duration = 1.0f;
[self addChild:explosion z:11];
[explosion release];
monsterHit = TRUE;
Monster *monster = (Monster *)target;
monster.hp--;
if (monster.hp <= 0) {
[targetsToDelete addObject:target];
[[SimpleAudioEngine sharedEngine] playEffect:@"splash.wav"];
_score ++;
}
break;
}
}
for (CCSprite *target in targetsToDelete) {
[_targets removeObject:target];
[self removeChild:target cleanup:YES];
}
if (targetsToDelete.count > 0) {
[ projectilesToDelete addObject:projectile];
}
[targetsToDelete release];
if (_score > _oldScore) {
_oldScore = _score;
[_scoreLabel setString:[NSString stringWithFormat:@"score%d", _score]];
[[NSUserDefaults standardUserDefaults] setInteger:_oldScore forKey:@"score"];
_score = 0;
}
}
- (void)update:(ccTime)dt {
NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
CGRect projectileRect = CGRectMake(
projectile.position.x - (projectile.contentSize.width/2),
projectile.position.y - (projectile.contentSize.height/2),
projectile.contentSize.width,
projectile.contentSize.height);
BOOL monsterHit = FALSE;
NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
for (CCSprite *target in _targets) {
CGRect targetRect = CGRectMake(
target.position.x - (target.contentSize.width/2),
target.position.y - (target.contentSize.height/2),
target.contentSize.width,
target.contentSize.height);
if (CGRectIntersectsRect(projectileRect, targetRect)) {
CCParticleFire* explosion = [[CCParticleFire alloc] initWithTotalParticles:200];
explosion.texture =[[CCTextureCache sharedTextureCache] addImage:@"sun.png"];
explosion.autoRemoveOnFinish = YES;
explosion.startSize = 20.0f;
explosion.speed = 70.0f;
explosion.anchorPoint = ccp(0.5f,0.5f);
explosion.position = target.position;
explosion.duration = 1.0f;
[self addChild:explosion z:11];
[explosion release];
monsterHit = TRUE;
Monster *monster = (Monster *)target;
monster.hp--;
if (monster.hp <= 0) {
[targetsToDelete addObject:target];
[[SimpleAudioEngine sharedEngine] playEffect:@"splash.wav"];
_score ++;
}
break;
}
}
for (CCSprite *target in targetsToDelete) {
[_targets removeObject:target];
[self removeChild:target cleanup:YES];
}
if (targetsToDelete.count > 0) {
[projectilesToDelete addObject:projectile];
}
[targetsToDelete release];
if (_score > _oldScore) {
_oldScore = _score;
[_scoreLabel setString:[NSString stringWithFormat:@"score%d", _score]];
[[NSUserDefaults standardUserDefaults] setInteger:_oldScore forKey:@"score"];
_score = 0;
}
The game overlayer
.h file game over
@interface GameOverLayer : CCLayerColor {
CCLabelTTF *_label;
CCSprite * background;
int _score;
int _oldScore;
}
@property (nonatomic, retain) CCLabelTTF *label;
@end
@interface GameOverScene : CCScene {
GameOverLayer *_layer;
}
@property (nonatomic, retain) GameOverLayer *layer;
@end
.m file gameover
#import "GameOverLayer.h"
#import "HelloWorldLayer.h"
#import "MainMenuScene.h"
@implementation GameOverScene
@synthesize layer = _layer;
- (id)init {
if ((self = [super init])) {
self.layer = [GameOverLayer node];
[self addChild:_layer];
}
return self;
}
- (void)dealloc {
[_layer release];
_layer = nil;
[super dealloc];
}
@end
@implementation GameOverLayer
@synthesize label = _label;
-(id) init
{
if( (self=[super initWithColor:ccc4(0,0,0,0)] )) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
self.label = [CCLabelTTF labelWithString:@"" fontName:@"Arial" fontSize:32];
_label.color = ccc3(225,0,0);
_label.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:_label];
[self runAction:[CCSequence actions:
[CCDelayTime actionWithDuration:3],
[CCCallFunc actionWithTarget:self selector:@selector(gameOverDone)],
nil]];
_score=0;
}