1

I have recently discovered that the CCProgressTimer class is replaced with CCProgressNode in the latest version of cocos2d, however, when i tried to implement the following code, there is nothing happen to the progressNode I have read all kinds of documentation, it seems like I have used all the latest methods. This all happens in gamePlay Class This is how I define the node.

CCProgressNode *_healthBar;
float _life;

This is the setUp method

- (void)initializeHealthBar {
    self->_healthBar = [[CCProgressNode alloc] init]; // This wasn't there before, but thought   it might be the memory allocation problem,
    // _health is the code connection between sprite builder and Xcode.
    self->_healthBar = [CCProgressNode progressWithSprite:_health]; 
    [_healthBar setType:CCProgressNodeTypeBar];
    [_healthBar setPercentage:_life];
    [_healthBar setBarChangeRate:ccp(0.1,0.1)];
    _healthBar.position = _health.position;
    // So the _healthBar turns out positioned correctly, because _health is already positioned in sprite builder
    [_contentNode addChild:_healthBar];

}

This is how i Involk the change on health bar... (It works, the healthBar is depleting... )

-(void) hit {
    if(_healthBar.percentage > 0)
    {
    self->_life -= 34;
    self->_healthBar.percentage -= 34;
    }
    if (self->_healthBar.percentage <= 0 && ![_myHero isGameOver]) {
        self->_healthBar.percentage = 0;
        [_myHero isGameOver: TRUE];
        [self gameOver];
    }
}
Bill Lau
  • 15
  • 5
  • In obj c you don't use `self->_life` for `ivars`. You just reference them like `life`. Also you call an `alloc init` and then create a new object with `progressWithSprite` in essence creating 2 objects. First one gets deleted, but still. I am editing your question to make your code and text more generic so people can focus on the `CCProgressBar` parts of the code. – Tibor Udvari Apr 18 '14 at 00:03

1 Answers1

3

I do not completely understand your problem, I have just written a small working example for a Left -> Right Bar Type Progress Bar

- (void) onEnter
{
    [super onEnter];

    CCSprite *sprite = [CCSprite spriteWithImageNamed:@"emma.png"];
    _progressNode = [CCProgressNode progressWithSprite:sprite];
    _progressNode.type = CCProgressNodeTypeBar;
    _progressNode.midpoint = ccp(0.0f, 0.0f);
    _progressNode.barChangeRate = ccp(1.0f, 0.0f);
    _progressNode.percentage = 0.0f;

    _progressNode.positionType = CCPositionTypeNormalized;
    _progressNode.position = ccp(0.5f, 0.5f);
    [self addChild:_progressNode];

    self.userInteractionEnabled = YES;
}

- (void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    _progressNode.percentage += 10.0f;
}

Notice that the CCSprite is not added to the scene, you can't use SpriteBuilder for that one I'm afraid. (Unless you want to remove it from the parent but that gets a little messy)

Also, do all the setup before you call the percentage setter.

And the percentage is actually a double. Always check to make sure that there are no casting problems happening.

Tibor Udvari
  • 2,932
  • 3
  • 23
  • 39
  • 1
    Thank you ! It worked, I guess I wasn't finish setting up the midpoint and barChangeRate and didn't load sprite from file directly. Thank you for your quick responds too – Bill Lau Apr 18 '14 at 09:08
  • You are welcome, make sure to check out the headers of these files, they usually explain what the class does and you can see the public API, it can give you an idea of how to work things out next time. That is always the first place I check. There is no better documentation than the code itself! – Tibor Udvari Apr 18 '14 at 12:43