cocos2d-x游戏开发(十五)游戏加载动画loading界面

时间:2022-06-14 05:00:38

个人原创,欢迎转载:http://blog.csdn.net/dawn_moon/article/details/11478885

这个资源加载的loading界面demo是在玩客网做逆转三国的时候随手写的,虽然我在那只待了2个礼拜,但是也算参与了一个商业游戏项目了,学到不少东西。当时使用的cocos2d-x还是1.0版的,我用2.1.2的调试过了。

上图:

cocos2d-x游戏开发(十五)游戏加载动画loading界面

好了,很简单,代码有注释

上代码:

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h" using namespace cocos2d;
using namespace CocosDenshion; CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create(); // 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene
scene->addChild(layer); // return the scene
return scene;
} // on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
} m_iLoadIdex = 0; /////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it. // add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback) );
pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) ); // create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition( CCPointZero );
this->addChild(pMenu, 1); /////////////////////////////
// 3. add your codes below... // add a label shows "Hello World"
// create and initialize a label
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34); // ask director the window size
CCSize size = CCDirector::sharedDirector()->getWinSize(); // position the label on the center of the screen
pLabel->setPosition( ccp(size.width / 2, size.height - 20) ); // add the label as a child to this layer
this->addChild(pLabel, 1); // add "HelloWorld" splash screen"
CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // position the sprite on the center of the screen
pSprite->setPosition( ccp(size.width/2, size.height/2) ); // add the sprite as a child to this layer
this->addChild(pSprite, 0); // loading边框
m_progressFrame = CCSprite::create("loading_fr.png");
addChild(m_progressFrame,1);
m_progressFrame->setPosition(ccp(240, 50)); // loading的动作条
m_progressBar = CCProgressTimer::create(CCSprite::create("loading_bar.png"));
m_progressBar->setType(kCCProgressTimerTypeBar);
addChild(m_progressBar);
m_progressBar->setVisible(true);
m_progressBar->setPosition(ccp(241, 51));
// 进度动画运动方向,从左到右
m_progressBar->setMidpoint(ccp(0, 0));
// 宽高变化,这里是宽度变化
m_progressBar->setBarChangeRate(ccp(1, 0));
m_progressBar->setPercentage(0); // loading动画,没有逻辑处理,实际情况则注释掉
CCProgressTo *to = CCProgressTo::create(10, 100);
m_progressBar->runAction(to); // 实际的loading逻辑,可以在这里加入
// scheduleUpdate();
return true;
} void HelloWorld::update(float dt)
{ m_iLoadIdex++; if (m_iLoadIdex <= 50) {
loadResource(m_iLoadIdex);
m_progressBar->setPercentage(m_iLoadIdex * 100.0 / 50);
} if (m_iLoadIdex >=50) {
unscheduleUpdate();
}
} // 实际的loading逻辑,加载资源可以在这里添加。
// 这里只用了sleep来模拟
void HelloWorld::loadResource(int index)
{
CCLog("loading ....");
switch (index) {
case 0:
break;
default:
sleep(1.0);
break;
}
} void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}

东西太简单,还需要上传demo源码么。要的留言
cocos2d-x游戏开发(十五)游戏加载动画loading界面