简体   繁体   English

更新分数Cocos2d 3.0

[英]Update Score Cocos2d 3.0

Ive been trying to figure out how to update the score. 我一直想弄清楚如何更新分数。 I have an label with a string that the score goes on but it doesn't update 我有一个带有字符串的标签,该分数会继续,但不会更新

This is the label with the score thats suppose to update 这是标记,其中包含要更新的分数

score=0;
CCLabelTTF *scorelabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"score: %d",score] fontName:@"Verdana-Bold" fontSize:18.0f];
scorelabel.positionType = CCPositionTypeNormalized;
scorelabel.color = [CCColor blackColor];
scorelabel.position = ccp(0.85f, 0.95f); // Top Right of screen
[self addChild:scorelabel];

Then this is where the score is added after a collision between two sprites 然后这是两个精灵碰撞后添加分数的地方

- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair monsterCollision:(CCNode *)monster projectileCollision:(CCNode *)projectile {
//Creating another sprite on the position the monster one was.
CCSprite *explosion = [CCSprite spriteWithImageNamed:@"explosion.png"];
explosion.position = monster.position;
[self addChild:explosion];
[[OALSimpleAudio sharedInstance] playEffect:@"exsound.mp3"];

CCActionDelay *delay = [CCActionDelay actionWithDuration:.0f];
CCActionFadeOut *fade = [CCActionFadeOut actionWithDuration:.4f];
[explosion runAction:[CCActionSequence actionWithArray:@[delay,fade]]];

[monster removeFromParent];
[projectile removeFromParent];

score++;

return YES;
}

And advise onto how i could update it because the scoreLabel refuses to update after a collision has been detected 并建议我如何更新它,因为scoreLabel在检测到碰撞后拒绝更新

Thank you :D 谢谢你:D

You need to update the scoreLabel where you update score. 您需要更新更新分数的scoreLabel。

So after , 之后,

 score++;

Include 包括

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

To update the score 要更新分数

@Implement (at the very top) @Implement(在最顶端)

CCLabelTTF *scorelabel;

Label displaying the Score 显示分数的标签

score=0;
scorelabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"score: %d",score] fontName:@"a2203.ttf" fontSize:18.0f];
scorelabel.positionType = CCPositionTypeNormalized;
scorelabel.color = [CCColor blackColor];
scorelabel.position = ccp(0.85f, 0.95f); // Top Right of screen
[self addChild:scorelabel];

After

score++;

Write

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM