2012-08-11 68 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