CCProgressNode for Health Bar无效

时间:2014-04-17 18:48:00

标签: ios objective-c cocos2d-iphone spritebuilder

我最近发现在最新版本的cocos2d中,CCProgressTimer类被CCProgressNode取代, 但是,当我尝试实现以下代码时,progressNode没有任何反应 我已阅读各种文档,似乎我已经使用了所有最新的方法。 这一切都发生在gamePlay Class中 这就是我定义节点的方式。

CCProgressNode *_healthBar;
float _life;

这是setUp方法

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

}

这就是我在健康栏上发生变化的方式......(它有效,健康酒吧正在耗尽......)

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

1 个答案:

答案 0 :(得分:3)

我不完全理解你的问题,我刚刚为左 - &gt;写了一个小的工作示例右栏类型进度条

- (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;
}

请注意,CCSprite未添加到场景中,您不能将SpriteBuilder用于我担心的那个。 (除非你想从父母那里删除它,但这有点混乱)

此外,在调用百分比设置器之前,请执行所有设置。

percentage实际上是double。务必检查以确保没有发生铸造问题。