1

最近发现在最新版本的cocos2d中CCProgressTimer类被替换为CCProgressNode了,但是,当我尝试实现下面的代码时,progressNode没有任何反应我看了各种文档,好像我有使用了所有最新的方法。这一切都发生在 gamePlay 类这就是我定义节点的方式。

CCProgressNode *_healthBar;
float _life;

这是设置方法

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

1 回答 1

3

我不完全理解您的问题,我刚刚为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;
}

请注意,没有添加到场景中,恐怕CCSprite你不能用于那个。SpriteBuilder(除非您想将其从父级中删除,但这会有点混乱)

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

percentage实际上是一个double. 始终检查以确保没有发生铸造问题。

于 2014-04-18T00:00:11.713 回答