Search code examples
ios6cocos2d-iphonexcode4.5

How to decrease CCProgressTimer by 10 percent incrementally in cocos2d?


In my app, I want my "health bar" to decrease by 10 percent out of 100 percent each time my sprite collides with another sprite. I already have the collision stuff down. How would I do this? Here is the code in my init to create the progress timer:

    CCSprite *healthFull = [CCSprite spriteWithFile:@"Health Bar.png"];
    CCSprite *healthBorder = [CCSprite spriteWithFile:@"Health Bar Border.png"];
    [self addChild:healthBorder z:5];
    healthBorder.position = ccp(size.width / 2, 300);
    health = [CCProgressTimer progressWithSprite:healthFull];
    health.type = kCCProgressTimerTypeBar;
    health.midpoint = ccp(1,0); // starts from right
    health.barChangeRate = ccp(-1,0); // grow only in the "x"-horizontal direction
    health.percentage = 100; // (0 - 100)
    [self addChild:health];
    health.position = ccp(size.width /2, 300);

And this is where I want to incrementally decrease the progress timer from 100 percent by 10 percent:

-(void)subtractLife{

 }

Solution

  • -(void)subtractLife{
         health.percentage-=10;
    
        if(health.percentage<0)
            health.percentage = 0;
    
     }