Cocos2d-x CCProgressTimer

前端之家收集整理的这篇文章主要介绍了Cocos2d-x CCProgressTimer前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Cocos2d-x CCProgressTimer

@H_404_3@CCProgressTimer,创建使用这个节点可以大致实现两个作用的效果

@H_404_3@其一:在游戏中几乎大部分的游戏启动界面都是游戏加载画面,那么用到的一般是进度条提示加载进度,其使用的就是CCProgressTimer。

@H_404_3@其二:在游戏中需要对精灵的出现等动作制作一些渐显的效果

@H_404_3@

@H_404_3@(1)类型一般就是两种:

@H_404_3@

  1. typedefenum{
  2. ///RadialCounter-Clockwise
  3. kCCProgressTimerTypeRadial,
  4. ///Bar
  5. kCCProgressTimerTypeBar,0); background-color:inherit">}CCProgressTimerType;
@H_404_3@
(2)类型1:radial(环形)

@H_404_3@

@H_404_3@

CCSizewSize=CCDirector::sharedDirector()->getWinSize();
  • progressTimer=CCProgressTimer::create(CCSprite::create("progress.gif"));
  • progressTimer->setType(kCCProgressTimerTypeRadial);
  • //默认的情况下,环形渐变的方向是:顺时针
  • //改变其渐变的方向MakestheridialCCW(逆时针)
  • progressTimer->setReverseProgress(true);
  • progressTimer->setPosition(wSize.width/2,wSize.height/2);
  • this->addChild(progressTimer);
  • @H_404_3@

    @H_404_3@
    (3)类型2:bar (条形:包括vertical 和 horizontal)

    渐变的方向问题:

    @H_404_3@vertical竖直方法包括从上到下和从下到上;

    @H_404_3@horizontal水平方向包括从左到右和从右到左。

    @H_404_3@这里涉及到两个设置参数:

    @H_404_3@首先是setMidpoint设置起点

    @H_404_3@

    /**
  • *Midpointisusedtomodifytheprogressstartposition.
  • *Ifyou'reusingradialstypethenthemidpointchangesthecenterpoint
  • *Ifyou'reusingbartypethethemidpointchangesthebargrowth
  • *itexpandsfromthecenterbutclampstothespritesedgeso:
  • *youwantalefttorightthensetthemidpointallthewaytoccp(0,y)
  • *youwantarighttoleftthensetthemidpointallthewaytoccp(1,y)
  • *youwantabottomtotopthensetthemidpointallthewaytoccp(x,0)
  • *youwantatoptobottomthensetthemidpointallthewaytoccp(x,1)
  • */
  • @H_404_3@
    其次是setBarChangeRate设置变化rate

    @H_404_3@

    @H_404_3@

    *Thisallowsthebartypetomovethecomponentataspecificrate
  • *Setthecomponentto0tomakesureitstaysat100%.
  • *Forexampleyouwantalefttorightbarbutnothavetheheightstay100%
  • *Settheratetobeccp(0,1);andsetthemidpointto=ccp(0,.5f);
  • */
  • @H_404_3@
    如果不用变化的方向,则设置该方向为0,否则设置为1。

    @H_404_3@

    @H_404_3@

    progressTimer->setType(kCCProgressTimerTypeBar);
  • //从左到右
  • progressTimer->setMidpoint(ccp(0,0.5));
  • progressTimer->setBarChangeRate(ccp(1,0));
  • //从右到左
  • //progressTimer->setMidpoint(ccp(1,0.5));
  • //progressTimer->setBarChangeRate(ccp(1,0));
  • //从上到下
  • //progressTimer->setMidpoint(ccp(0.5,1));
  • //progressTimer->setBarChangeRate(ccp(0,1));
  • //从下到上
  • this->addChild(progressTimer);
  • @H_404_3@
    (4) 执行变化

    @H_404_3@

    @H_404_3@①、如果是要实现精灵渐变的显示效果

    @H_404_3@创建CCProgressTo或者是CCProgressFromTo动作,让CCProgressTimer执行。
    CCProgressTo和CCProgressFromTo的区别是:

    @H_404_3@前者:Progress to percentage(初始化有两个参数)(floatduration,floatfPercent)

    @H_404_3@后者:Progress from a percentage to another percentage(初始化有三个参数)(floatduration,floatfFromPercentage,floatfToPercentage)

    @H_404_3@

    CCProgressTo*progressTo=CCProgressTo::create(2.0,100);
  • //等价于:
  • //CCProgressFromTo*progressFromTo=CCProgressFromTo::create(2.0,100);
  • progressTimer->runAction(CCRepeatForever::create(progressTo));
  • @H_404_3@
    ②、如果是要实现加载进度条的效果

    @H_404_3@

    @H_404_3@需要重载update方法,在这个方法中实现进度条percentage的变化。

    @H_404_3@

    this->scheduleUpdate();
    @H_404_3@

    voidHelloWorld::update(floatdt)
  • {
  • floatpercentage=progressTimer->getPercentage();
  • if(percentage<100){
  • percentage+=1;
  • progressTimer->setPercentage(percentage);
  • }
  • }
  • @H_404_3@

    @H_404_3@

    @H_404_3@关于CCProgressTimer的更加详细的使用 demo可以参看引擎中sample中的ActionProgressTest。

    猜你在找的Cocos2d-x相关文章