CCProgressTimer是Cocos2d中用于显示进度的派生类,刚在使用中发现Cocos2d-1.0与Cocos2d-2.0中关于CCProgressTimer的功能有些区别:
首先是type,在2.0中CCProgressTimer的type比1.0中少了很多,1.0中有:
typedef enum {
/// Radial Counter-Clockwise
kCCProgressTimerTypeRadialCCW,
/// Radial ClockWise
kCCProgressTimerTypeRadialCW,
/// Horizontal Left-Right
kCCProgressTimerTypeHorizontalBarLR,
/// Horizontal Right-Left
kCCProgressTimerTypeHorizontalBarRL,
/// Vertical Bottom-top
kCCProgressTimerTypeVerticalBarBT,
/// Vertical Top-Bottom
kCCProgressTimerTypeVerticalBarTB,
} CCProgressTimerType;
2.0中只保留了两个常用的:
typedef enum {
/// Radial Counter-Clockwise
kCCProgressTimerTypeRadial,
/// Bar
kCCProgressTimerTypeBar,
} CCProgressTimerType;
其次CCProgressTimer的初始化也略有不同,在1.0中是通过CCProgressTimer *timer=[CCProgressTimer progressWithFile:“文件名.png”]来初始化的,通过查看cocos2d-2.0的源代码,显然没有了progressWithFile这个方法,取而代之的是progressWithSprite,这就需要先将要显示为进度条的图像文件创建CCSprite,即
CCSprite *sprite=[CCSprite spriteWithFile:@"文件名.png"];
CCProgressTimer* timer = [CCProgressTimer progressWithSprite:sprite];
虽然类型少了但是之前的功能还是能实现的,这里有一个新属性:reverseDirection(bool),这个是用来改变进度条的播放进度的方向的。
举例说明:
CCSprite *sprite1 = [CCSprite spriteWithFile:@"Icon.png"];
//CCSprite *sprite2 = [CCSprite spriteWithFile:@"Icon.png"];
//CCSprite *sprite3 = [CCSprite spriteWithFile:@"Icon.png"];
// 1.CCProgressTimer : 进度动画的渲染器,核心实现了进度动画的显示功能,本身是一个结点,通过渲染一个精灵来表现进度的变化。
// 2.CCProgressTo: TO进度控制器,控制进度从当前进度变化到某个值.
// 3.CCProgressFromTo: FromTo进度控制器,控制进度从一个指定值到另一个值的变化.
// CCProgressTimer
timer = [CCProgressTimer progressWithSprite:sprite1];
//timer.opacity = 50;
timer.type = kCCProgressTimerTypeRadial;
timer.reverseDirection = YES;
timer.percentage = 0.0f;
timer.position = ccp(60, 150);
[self addChild:timer];
timer1 = [CCProgressTimer progressWithSprite:sprite1];
//timer1.opacity = 50;
timer1.type = kCCProgressTimerTypeBar;
//timer1.reverseDirection = YES;
timer1.percentage = 0.0f;
timer1.position = ccp(160, 150);
[self addChild:timer1];
timer2 = [CCProgressTimer progressWithSprite:sprite1];
//timer2.opacity = 50;
timer2.type = kCCProgressTimerTypeBar;
timer2.reverseDirection = YES;
timer2.percentage = 0.0f;
timer2.position = ccp(260, 150);
[self addChild:timer2];
timer3 = [CCProgressTimer progressWithSprite:sprite1];
//timer2.opacity = 50;
timer3.type = kCCProgressTimerTypeRadial;
//timer3.reverseDirection = YES;
timer3.percentage = 0.0f;
timer3.position = ccp(360, 150);
[self addChild:timer3];
[self schedule:@selector(updateTimer) interval:0.2f];
-(void)updateTimer
{
timer.percentage += 1;
timer1.percentage += 1;
timer2.percentage += 1;
timer3.percentage += 1;
// [CCProgressFromTo actionWithDuration:<#(ccTime)#> from:<#(float)#> to:<#(float)#>];
// [CCProgressFromTo actionWithDuration:<#(ccTime)#>];
// [CCProgressTo actionWithDuration:<#(ccTime)#>];
// [CCProgressTo actionWithDuration:<#(ccTime)#> percent:<#(float)#>];
}
不过2.0少了横向进度条,应该是应为横行进度条的实现很简单,你只要更新一下scaleX这个属性就行了