cocos2d-x学习笔记(8)--progress(进度条)
cocos2d-x学习笔记(7)--progress(进度条)
在一些游戏中,我们会见到游戏载入进度,或者在游戏统计成绩时,见到一些饼状图。下面我们就来学习进度条。
这次要学习两个类:
1.CCProgressTo
2.CCProgressTimer
在cocos2d-x 的online api document中我们可以看到CCProgressTo的继承关系:
就是说CCProgressTo其实就是设置动作的一个类,只不过多了进度条特有的属性。
step1:创建cocos2d-win32 application,并命名为progress
step2:在HelloWorldScene.h中添加如下几个类:
[cpp]
class SpriteDemo:public CCLayer
{
public:
virtual std::string title();
virtual void onEnter();
void backCallback(CCObject* pSender);
void restartCallback(CCObject* pSender);
void nextCallback(CCObject*pSender);
};
class SpriteProgressToRadial : public SpriteDemo
{
public:
virtual void onEnter();
virtual std::string title();
};
class SpriteProgressToHorizontal : public SpriteDemo
{
public:
virtual void onEnter();
virtual std::string title();
};
class SpriteProgressToVertical : public SpriteDemo
{
public:
virtual void onEnter();
virtual std::string title();
};
在HelloWorldScene.cpp中修改scene函数:
[cpp] view plaincopy
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::node();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
SpriteProgressToRadial* layer = new SpriteProgressToRadial();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
} while (0);
// return the scene
return scene;
}
同时添加如下函数:
[cpp]
/************************************************************************/
/* SpriteDemo */
/************************************************************************/
std::string SpriteDemo::title()
{
return "ProgressActionsTest";
}
void SpriteDemo::onEnter()
{
CCLayer::onEnter();
CCSize size = CCDirector::sharedDirector()->getWinSize();
//添加标题
std::string strTitle = title();
const char* ptitle = strTitle.c_str();
CCLabelTTF* label = CCLabelTTF::labelWithString(ptitle,"Arial", 28);
label->setPosition(ccp(size.width/2, size.height-30));
addChild(label);
//添加基本按钮
CCLabelTTF* pNextLabel = CCLabelTTF::labelWithString("Next ", "Arial", 28);
CCLabelTTF*pBackLabel = CCLabelTTF::labelWithString("Back ", "Arial", 28);
CCLabelTTF*pRestartLabel = CCLabelTTF::labelWithString("Restart ", "Arial", 28);
CCMenuItemLabel* pNextItem = CCMenuItemLabel::itemWithLabel(
pNextLabel, this, menu_selector(SpriteDemo::nextCallback));
CCMenuItemLabel* pBackItem = CCMenuItemLabel::itemWithLabel(
pBackLabel, this, menu_selector(SpriteDemo::backCallback));
CCMenuItemLabel* pRestartItem = CCMenuItemLabel::itemWithLabel(
pRestartLabel, this, menu_selector(SpriteDemo::restartCallback));
CCMenu* pNextMenu = CCMenu::menuWithItem(pNextItem);
CCMenu* pBackMenu = CCMenu::menuWithItem(pBackItem);
CCMenu* pRestartMenu = CCMenu::menuWithItem(pRestartItem);
pNextItem->setPosition(ccp(size.width/2 +150, 50));
pBackItem->setPosition(ccp(size.width/2 - 150, 50));
pRestartItem->setPosition(ccp(size.width/2 , 50));
pNextMenu->setPosition(CCPointZero);
pBackMenu->setPosition(CCPointZero);
pRestartMenu->setPosition(CCPointZero);
addChild(pNextMenu,1);
addChild(pBackMenu, 1);
addChild(pRestartMenu,1);
}
void SpriteDemo::backCallback(CCObject* pSender)
{
if(index == 1)
return;
index--;
CCScene* scene = new CCScene();
CCLayer* layer = runThisTest(index);
scene->addChild(layer);
CCDirector::sharedDirector()->replaceScene(scene);
scene->release();
layer->release();
}
void SpriteDemo::restartCallback(CCObject* pSender)
{
CCScene* scene = new CCScene();
CCLayer* layer = runThisTest(index);
scene->addChild(layer);
CCDirector::sharedDirector()->replaceScene(scene);
scene->release();
layer->release();
}
void SpriteDemo::nextCallback(CCObject* pSender)
{
if(index == MAX_INDEX)
return;&nbs
补充:移动开发 , 其他 ,