cocos2d-x学习笔记(8)--progress(进度条)

cocos2d-x学习笔记(8)--progress(进度条)

本文出自http://www.wenbanana.com稻草人博客,欢迎访问!

       在一些游戏中,我们会见到游戏载入进度,或者在游戏统计成绩时,见到一些饼状图。下面我们就来学习进度条。

这次要学习两个类:

1.CCProgressTo

2.CCProgressTimer

在cocos2d-x 的online api document中我们可以看到CCProgressTo的继承关系:

cocos2d-x学习笔记(8)--progress(进度条)_第1张图片

就是说CCProgressTo其实就是设置动作的一个类,只不过多了进度条特有的属性。

step1:创建cocos2d-win32 application,并命名为progress

step2:在HelloWorldScene.h中添加如下几个类:

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函数:

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;
}


同时添加如下函数:

/************************************************************************/
/* 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;
	index++;
	CCScene* scene = new CCScene();
	CCLayer* layer = runThisTest(index);
	scene->addChild(layer);
	CCDirector::sharedDirector()->replaceScene(scene);
	scene->release();
	layer->release();
}


/************************************************************************/
/* SpriteProgressToRadial                                                                     */
/************************************************************************/

std::string SpriteProgressToRadial::title()
{
	return "SpriteProgressToRadial";
}

void SpriteProgressToRadial::onEnter()
{
	SpriteDemo::onEnter();

	CCSize size = CCDirector::sharedDirector()->getWinSize();

	CCProgressTo* p1 = CCProgressTo::actionWithDuration(2,80);
	//第一个参数是时间,第二个参数是表示只旋转80%

	CCProgressTimer* pt = CCProgressTimer::progressWithFile("radial.png");
	pt->setType(kCCProgressTimerTypeRadialCW);
	addChild(pt);
	pt->setPosition(ccp(size.width / 2, size.height / 2));
	pt->runAction(CCRepeatForever::actionWithAction(p1));
}

/************************************************************************/
/* SpriteProgressToHorizontal                                                                     */
/************************************************************************/

std::string SpriteProgressToHorizontal::title()
{
	return "SpriteProgressToHorizontal";
}

void SpriteProgressToHorizontal::onEnter()
{
	SpriteDemo::onEnter();

	CCSize size = CCDirector::sharedDirector()->getWinSize();

	CCProgressTo* to = CCProgressTo::actionWithDuration(2,100);
	CCProgressTimer*pt = CCProgressTimer::progressWithFile("horizonal.png");
	pt->setType(kCCProgressTimerTypeHorizontalBarLR);
	addChild(pt);
	pt->setPosition(ccp(size.width / 2, size.height / 2));
	pt->runAction(CCRepeatForever::actionWithAction(to));
}


/************************************************************************/
/* SpriteProgressToVertical                                                                     */
/************************************************************************/

std::string SpriteProgressToVertical::title()
{
	return "SpriteProgressToVertical";
}

void SpriteProgressToVertical::onEnter()
{
	SpriteDemo::onEnter();

	CCSize size = CCDirector::sharedDirector()->getWinSize();
	CCProgressTo* to = CCProgressTo::actionWithDuration(2,100);
	CCProgressTimer*pt = CCProgressTimer::progressWithFile("vertical.png");
	pt->setType(kCCProgressTimerTypeVerticalBarBT);
	pt->setPosition(ccp(size.width / 2, size.height / 2));
	addChild(pt);
	pt->runAction(CCRepeatForever::actionWithAction(to));

}


step3:运行程序,会见到如下三种效果:

圆饼效果:

cocos2d-x学习笔记(8)--progress(进度条)_第2张图片

水平效果

cocos2d-x学习笔记(8)--progress(进度条)_第3张图片

 

垂直效果:

cocos2d-x学习笔记(8)--progress(进度条)_第4张图片

 

 源代码下载地址:http://download.csdn.net/download/wen294299195/4525813

 

你可能感兴趣的:(游戏,String,application,Class,menu,layer)