我正在使用CCProgressTimer为HP吧编写游戏。
以下是添加HP栏的代码:
_hpBar = [CCProgressTimer progressWithSprite:[CCSprite spriteWithFile:@"hp_bar.png"]];
_hpBar.type = kCCProgressTimerTypeBar;
_hpBar.barChangeRate = ccp(1, 0); // Y stays the same
_hpBar.midpoint = ccp(0, 0);
_hpBar.percentage = 100;
_hpBar.position = ccp(_hpBar.contentSize.width * scale / 2, self.contentSize.height + _hpBar.contentSize.height / 2 + 2);
[self addChild:_hpBar];
添加这是我增加HP的代码:
- (int)increaseHP:(int)amount {
_life = MIN(_life + amount, 100);
if (_life > 30) [_hpBar setSprite:[CCSprite spriteWithFile:@"hp_bar.png"]];
[_hpBar setPercentage:_life];
return _life;
}
然而,当HP已满时,即_life = 100,然后我再增加一些HP,即调用[self increaseHP:1],HP栏将消失。
有人可以帮忙吗?
答案 0 :(得分:0)
因为百分比从0到100,并且> 100%的行为未定义(不受保护)。
如果您的角色可以拥有,例如最大250 HP,那么您需要计算角色拥有的健康百分比。如果他有125马力就可以达到50%。