Question

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];
    }
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top