Cocos2d基于CCProgressTimer的倒计时进度条

最近学习Cocos2d,碰到一个问题就是需要使用一个进度条(倒计时)来显示游戏剩余时间。

查阅了相关文档,实现起来还是非常简单的。使用到了CCProgressTimer对象,实现代码如下:


#import "ProgressBar.h"
 
@implementation ProgressBar
 
// 场景
+ (id) scene
{
	CCLOG(@"%@:%@", NSStringFromSelector(_cmd), self);
 
	CCScene *scene = [CCScene node];
	ProgressBar *layer = [ProgressBar node];
	[scene addChild: layer];
	return scene;
}
 
// 初始化场景
- (id) init
{
	if((self = [super init]))
	{
 
		// 屏幕尺寸
		CGSize screenSize = [[CCDirector sharedDirector] winSize];
 
		// 进度条背景
		CCSprite *progressBg = [CCSprite spriteWithFile:@"progressbg.png"];
		progressBg.position = ccp(screenSize.width / 2, screenSize.height / 2);
		[self addChild:progressBg z:1];
 
		// 进度条
		CCProgressTimer *timer = [CCProgressTimer progressWithFile:@"progress.png"];
		CCSequence *timerAction = [CCSequence actions:[CCProgressTo actionWithDuration:100 percent:100.f], nil];
		timer.type = kCCProgressTimerTypeHorizontalBarRL;
		timer.position = ccp(screenSize.width / 2, screenSize.height / 2);
		timer.percentage = 0.0f;
		[self addChild:timer z:2];
		[timer runAction:timerAction];
 
		// 背景图片
		CCSprite *bg = [CCSprite spriteWithFile:@"background.png"];
		bg.position = ccp(screenSize.width / 2, screenSize.height / 2);
		[self addChild:bg z:-1 tag:1];
	}
	return self;
}
@end


kCCProgressTimerTypeHorizontalBarRL为进度条显示方式,表示从右往左。

一共提供了6种进度条处理的 方法 ,顺时针,逆时针,从左到右,从右到左,从上到下,从左到右

typedef enum {
kProgressTimerTypeRadialCCW,
kProgressTimerTypeRadialCW,
kProgressTimerTypeHorizontalBarLR,
kProgressTimerTypeHorizontalBarRL,
kProgressTimerTypeVerticalBarBT,
kProgressTimerTypeVerticalBarTB,
} ProgressTimerType;

actionWithDuration 参数为倒计时时长(单位:秒),这里定义为100秒倒计时结束。

原文地址:http://hxsdit.com/993

你可能感兴趣的:(Cocos2d基于CCProgressTimer的倒计时进度条)