CCProgressTimer在cocos2d 2.0中的改变

CCProgressTimerCocos2d中用于显示进度的派生类,刚在使用中发现Cocos2d-1.0Cocos2d-2.0中关于CCProgressTimer的功能有些区别:

首先是type,在2.0CCProgressTimertype1.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这个属性就行了


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cocos2d-x 2.7 版本,你可以使用 `CCProgressTimer` 类来创建一个进度条来显示数据加载的进度。具体的实现步骤如下: 1. 创建一个进度条对象 ``` CCProgressTimer* progress = CCProgressTimer::create(CCSprite::create("progressbar.png")); progress->setType(kCCProgressTimerTypeBar); progress->setMidpoint(ccp(0, 0.5)); progress->setBarChangeRate(ccp(1, 0)); progress->setPosition(ccp(winSize.width / 2, winSize.height / 2)); this->addChild(progress); ``` 其,`progressbar.png` 是进度条的图片,可以自己定义。`setType` 方法设置进度条的类型为水平条,`setMidpoint` 方法设置进度条的起点为左侧间位置,`setBarChangeRate` 方法设置进度条的变化速率为水平方向。 2. 加载数据时更新进度条 在加载数据时,可以根据已经加载的数据量和总数据量来计算加载进度,并将进度条的百分比设置为相应的值。 ``` // 加载数据的过程 int loadedData = 0; // 已经加载的数据量 int totalData = 100; // 总数据量 float percent = (float)loadedData / totalData * 100; // 计算进度百分比 progress->setPercentage(percent); // 设置进度条的百分比 ``` 其,`loadedData` 表示已经加载的数据量,`totalData` 表示总数据量,`percent` 表示已经加载的数据量占总数据量的百分比,`setPercentage` 方法将进度条的百分比设置为相应的值。 3. 加载完成时隐藏进度条 当数据加载完成时,可以将进度条隐藏起来。 ``` // 数据加载完成后 progress->setVisible(false); // 隐藏进度条 ``` 这样,你就可以在 Cocos2d-x 2.7 使用 `CCProgressTimer` 类来创建一个数据加载进度条了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值