How to make and correctly update progress bar in cocos2d?

Viewed 7092

I have a game that uses a progress bar to inform player of level of certain stats of the player. For example hunger, when it starts at zero and slowly adds up to maximum bar. When he eats the hunger reduces.

I tried implementing as progressBar, but it works wrong, as the bar expands both ways, and I need it to grow one side only. Also I had hard time setting the bar, since it uses actions.

Is there an easy way to do it?

I have a class Pet and it has int hunger (0-100). I want the bar to be showing hunger.

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];

Added source.

1 Answers
Related