如何设置CC Progress Timer sprite的动画?

时间:2014-01-07 09:03:26

标签: ios cocos2d-iphone

有没有办法动画CCProgressTimer精灵? 我试过但它不起作用:(

if (timeCountdown <= 10) {


    CCAnimation *animation = [[CCAnimation alloc] init];
    [animation addSpriteFrameWithFilename:@"time-bar.png"];
    [animation addSpriteFrameWithFilename:@"time-bar-white.png"];
    [animation setDelayPerUnit:0.02f];
    [animation setLoops:1];
    [progressTimer.sprite runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]]];
    }

当timeCountdown&lt; = 10时,我希望进度条永远闪烁。 感谢。

编辑:找到解决方案,但我不知道为什么哈哈 只需从parent中删除progressTimer.sprite,运行操作,然后再将其添加到图层:

if (timeCountdown <= 10) {
        int n = [progressTimer.sprite numberOfRunningActions];
        if (n == 0) {
            [time_bar removeFromParent];
            CCAnimation *animation = [[CCAnimation alloc] init];
            [animation addSpriteFrameWithFilename:@"time-bar.png"];
            [animation addSpriteFrameWithFilename:@"time-bar-white.png"];
            [animation setDelayPerUnit:0.02f];
            [animation setRestoreOriginalFrame:YES];
            id repeatAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]];
            [time_bar runAction:repeatAction];
            [self addChild:time_bar];
        }
    }
    else {
        [time_bar stopAllActions];
        [time_bar setTexture:[[CCTextureCache sharedTextureCache] addImage:@"time-bar.png"]];


    }

EDIT2:只需将精灵添加到图层,将Visible设置为NO,然后运行aciton。这是一个技巧,因为精灵如果不是图层的子节点就无法运行动作。

2 个答案:

答案 0 :(得分:0)

只需更改精灵的颜色属性即可。或定期改变其可见状态。

答案 1 :(得分:0)

试试这段代码:

//[animation setLoops:1];
[progressTimer stopAllActions];
[progressTimer.sprite runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]]];