转载自:http://blog.csdn.net/junhua_peng/article/details/17008533
1.创建:
CCProgresstime::create("精灵对象");
2.种类:
cocos2dx中,有2种计时器的样式:
[1].kCCProgressTimerTypeRadial:扇形进度计时器
[2].kCCProgressTimerTypeBar:条形进度计时器
3.函数:
setType(); //设置进度条样式
[扇形][1].setPercentage(); //设置进度值(0-100)
[扇形][2].setReverseProgress(); //设置反向计时(true为开启反向)
[条形][3].setBarChangeRate(); //修改条形计时器变换样式(定义起始样式)
ccp(1,0):只有X轴变化。 (起始X轴不显示)
ccp(0,1):只有Y轴变化。 (起始Y轴不显示)
ccp(1,1):X,Y轴都变化。 (起始X,y轴都不显示)
ccp(0.5,0.5):X,Y轴都变化 。(起始X,y轴都显示一半)
[条形][4].setMidpoint(); //设置计时器的显示方式方向,范围(0-1)(默认ccp(0.5,0.5))
注意:该函数会受到setBarChangeRate函数影响
当计时条样式为X轴变化时(设置Y轴无效果,这里设为0)
ccp(1,0): 从右到左显示。
ccp(0.5,0): 从中间到两边显示。
ccp(0,0): 从左到右显示
当计时条样式为Y轴变化时(设置X轴无效果,这里设为0)
ccp(0,1): 从上到下显示。
ccp(0,0.5): 从中间到两边显示。
ccp(0,0): 从下到上显示。
当计时条样式为X、Y轴都变化时
ccp(0,1): X从左到右显示Y从上到下显示
ccp(0,0.5): X从中间到两边显示Y从中间到两边显示
ccp(1,0): X从右到左显示Y从下到上显示
ccp(0.5,0): X从中间到两边显示Y从下到上显示
ccp(0,0): X从左到右显示Y从下到上显示
ccp(0.5,0.5): X从中间到两边显示Y从中间到两边显示
ccp(1,1): X从右到左显示Y从上到下显示
3.开始编码
- 1.包含“cocos-ext.h”文件。#include "cocos-ext.h"
- 2.引用命名空间“cocos2d::extension”。using namespace cocos2d::extension;
- 3.我们需2个回调函数用于按钮关联事件。
- void downaction1(CCObject * sender,CCControlEvent);
- void downaction2(CCObject * sender,CCControlEvent);
- 4.需2个回调函数用于建造完成关联事件。
- void deletesp();
- void deletesp2();
- <pre code_snippet_id="86784" snippet_file_name="blog_20131128_1_6597456" name="code" class="cpp">1.引用命名空间“cocos2d::extension”。using namespace cocos2d::extension;
- 2.放上背景。
- CCSprite* bgps = CCSprite::create("bg.jpg");
- bgps->setPosition(ccp(mysize.width/2, mysize.height/2));
- this->addChild(bgps, 0);
- 3.创建2个按钮。
- CCScale9Sprite * c91=CCScale9Sprite::create("button.png");
- CCScale9Sprite * c92=CCScale9Sprite::create("button.png");
- CCLabelTTF * ttf1=CCLabelTTF::create("Ji Di","Arial",20);
- CCLabelTTF * ttf2=CCLabelTTF::create("Dian Zhan","Arial",20);
- CCControlButton * mybutton1=CCControlButton::create(ttf1,c91);
- CCControlButton * mybutton2=CCControlButton::create(ttf2,c92);
- mybutton1->setPosition(ccp(mysize.width-50,mysize.height-140));
- mybutton2->setPosition(ccp(mysize.width-50,mysize.height-180));
- mybutton1->setPreferredSize(CCSizeMake(80,30));
- mybutton2->setPreferredSize(CCSizeMake(80,30));
- this->addChild(mybutton1);
- this->addChild(mybutton2);
- mybutton1->addTargetWithActionForControlEvents(
- this,
- cccontrol_selector(Progressdemo::downaction1),
- CCControlEventTouchDown
- );
- mybutton2->addTargetWithActionForControlEvents(
- this,
- cccontrol_selector(Progressdemo::downaction2),
- CCControlEventTouchDown
- );
- 4.实现按钮回调函数
- void Progressdemo::downaction1(CCObject * sender,CCControlEvent){
- //创建扇形进度条
- CCSize mysize=CCDirector::sharedDirector()->getWinSize();
- CCProgressTimer *mypro=CCProgressTimer::create(CCSprite::create("sjd.png"));
- mypro->setPosition(ccp(100,100));
- mypro->setType(kCCProgressTimerTypeRadial);
- mypro->setPercentage(100);
- this->addChild(mypro,100,1);
- //创建计时器
- CCProgressTo * to=CCProgressTo::create(5,100);
- CCCallFunc * callback=CCCallFunc::create(this,callfunc_selector(Progressdemo::deletesp));
- CCAction * act=CCSequence::create(to,callback,NULL);
- mypro->runAction(act);
- }
- void Progressdemo::downaction2(CCObject * sender,CCControlEvent){
- //创建条形进度条
- CCSize mysize=CCDirector::sharedDirector()->getWinSize();
- CCProgressTimer *mypro=CCProgressTimer::create(CCSprite::create("tjd.png"));
- mypro->setPosition(ccp(100,150));
- mypro->setType(kCCProgressTimerTypeBar);
- mypro->setBarChangeRate(ccp(1,0));
- mypro->setMidpoint(ccp(0,1));
- this->addChild(mypro,100,2);
- //创建计时器
- CCProgressTo * to=CCProgressTo::create(3,100);
- //这里为了实现执行完成后再操作需要用到回调函数
- CCCallFunc * callback=CCCallFunc::create(this,callfunc_selector(Progressdemo::deletesp2));
- CCAction * act=CCSequence::create(to,callback,NULL);
- mypro->runAction(act);
- }
- 5.实现建造回调函数
- void Progressdemo::deletesp(){
- CCProgressTimer *mypro=(CCProgressTimer *)this->getChildByTag(1);
- this->removeChild(mypro);
- CCSize mysize=CCDirector::sharedDirector()->getWinSize();
- CCSprite * jidi=CCSprite::create("jd.png");
- jidi->setPosition(ccp(100,100));
- //设置缩放倍数
- jidi->setScale(0.3f);
- this->addChild(jidi);
- }
- void Progressdemo::deletesp2(){
- CCProgressTimer *mypro=(CCProgressTimer *)this->getChildByTag(2);
- this->removeChild(mypro);
- CCSize mysize=CCDirector::sharedDirector()->getWinSize();
- CCSprite * fdz=CCSprite::create("fdz.png");
- fdz->setPosition(ccp(100,150));
- //设置缩放倍数
- fdz->setScale(0.3f);
- this->addChild(fdz);
- }
- </pre><br>
- <pre></pre>