cocos2d progress bar example

A progress bar implementation for my second game’s HUD layer to show how much rocket fuel is left:

	CCSprite * progressBorder = [CCSprite spriteWithFile: @ "progressbarborder.png"];
	[progressBorder setPosition:ccp(s.width/2 + 0.5 - 45, s.height - 20.5f)];
	[self addChild: progressBorder z:1]; 

	CCProgressTimer* progressBar = [CCProgressTimer progressWithFile:@"progressbar.png"];
	progressBar.type = kCCProgressTimerTypeHorizontalBarLR;

	[progressBorder addChild:progressBar_ z:1];
	[progressBar setAnchorPoint: ccp(0,0)];

To change the progress amount:

        CCProgressTo *progressTo = [CCProgressTo actionWithDuration:10 percent:100];
	[progressBar runAction:progressTo];

To use a callback after completion:

        CCCallFunc *cbDecrFinished = [CCCallFunc actionWithTarget:self selector:@selector(decreaseProgressBarFinished:)];
	CCProgressFromTo *progressToZero = [CCProgressFromTo actionWithDuration:10 from:100 to:0];
	CCSequence *asequence = [CCSequence actions:progressToZero, cbDecrFinished, nil]; 

	[progressBar runAction:asequence];

where decreaseProgressBarFinished may be defined as:

        - (void) decreaseProgressBarFinished:(id)sender;

One Response to cocos2d progress bar example

  1. Niels says:

    Thank you for this, just what I was looking for! 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: