2012-08-11 27 views
6

我有一個遊戲,使用進度條通知玩家某些屬性的水平。例如飢餓,當它從零開始,慢慢增加到最大值。當他吃飢餓減少。如何製作並正確更新cocos2d中的進度條?

我嘗試作爲progressBar實現,但它工作不正確,因爲酒吧擴展了兩種方式,我需要它只增長一面。此外,我很難設置欄,因爲它使用動作。

有沒有簡單的方法來做到這一點?

我有一個類寵物,它有詮釋飢餓(0-100)。我想讓酒吧顯示飢餓感。

hungerBar = [CCSprite spriteWithFile:@"redbar.png"]; 
    CCLabelTTF *hungerLabel = [CCLabelTTF labelWithString:@"Hunger:" fontName:@"Helvetica" fontSize:25]; 
    [hungerLabel setColor:ccc3(255, 255, 255)]; 

// CGPoint temp = ccp(250, 300); 
// hungerBar.position = temp; 
// [self addChild:hungerBar]; 
    CGPoint temp2 = ccp(250, 320); 
    [hungerLabel setPosition:temp2]; 
    [self addChild:hungerLabel]; 

    CCSprite *bar = [CCSprite spriteWithFile:@"redbar.png"]; 
    powerBar= [CCProgressTimer progressWithSprite:bar]; 
    powerBar.type = kCCProgressTimerTypeBar; 
    powerBar.position = ccp(-30, -10); 
    powerBar.anchorPoint = ccp(0, 0); 
    powerBar.percentage = 20; // (0 - 100) 
    [hungerLabel addChild:powerBar]; 

新增來源。

+0

你能夠顯示你的progressBar代碼嗎? – 2012-08-11 07:13:21

回答

15

在cocos2d 2.0之前,您應該只能使用類型爲:kCCProgressTimerTypeHorizo​​ntalBarLR的CCProgressTimer。

CCProgressTimer* powerBar= [CCProgressTimer progressWithFile:@"fullbar.png"]; 
powerBar.type = kCCProgressTimerTypeHorizontalBarLR; 
powerBar.percentage = 0; // (0 - 100) 

要改變你的飢餓水平,只需設置你的酒吧的百分比財產。

編輯:

好,用的cocos2d 2.0,似乎這樣的類型不再可用。爲了得到一個左到右的酒吧,你將需要設置新的,但有些混亂中點barChangeRate屬性(cocos2D 2.0 documentation link):

CCProgressTimer* powerBar= [CCProgressTimer progressWithSprite:[CCSprite spriteWithFile:@"fullbar.png"]]; 
powerBar.type = kCCProgressTimerTypeBar; 
powerBar.midpoint = ccp(0,0); // starts from left 
powerBar.barChangeRate = ccp(1,0); // grow only in the "x"-horizontal direction 
powerBar.percentage = 0; // (0 - 100) 

看看這些幫助!

+0

這在Cocos2d 2中不起作用。沒有像這樣的方法,只有帶精靈的方法,我做到了,它可以,但是酒吧增長的方式和我只需要一種方法。在cocos2d 2中也沒有這種類型,只有以下類型 - CCSprite * bar = [CCSprite spriteWithFile:@「redbar.png」]; powerBar = [CCProgressTimer progressWithSprite:bar]; powerBar.type = kCCProgressTimerTypeBar; – Dvole 2012-08-11 08:53:00

+0

是的,顯然他們刪除了2.0中的類型。看到我編輯的答案:) – 2012-08-11 09:24:29

+0

「編輯代碼」的作品,謝謝 – kamankily 2012-12-16 02:05:49