2013-05-26 54 views
0

我正在使用CCProgressTimer爲HP欄編寫遊戲。CCProgressTimer無法正常工作

以下是我的代碼添加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 increase HP:1],HP欄會消失。

任何人都可以幫忙嗎?

回答

0

因爲百分比從0到100,並且> 100%的行爲未定義(不受保護)。

如果你的角色可以擁有250 HP最大值,那麼你需要計算角色的健康百分比。例如,如果他有125惠普,例如,這將使50%。