个人原创,欢迎转载:http://www.jb51.cc/article/p-xryqknmf-zw.html
这个资源加载的loading界面demo是在玩客网做逆转三国的时候随手写的,虽然我在那只待了2个礼拜,但是也算参与了一个商业游戏项目了,学到不少东西。当时使用的cocos2d-x还是1.0版的,我用2.1.2的调试过了。
上图:
好了,很简单,代码有注释
上代码:
- #include"HelloWorldScene.h"
- #include"SimpleAudioEngine.h"
- usingnamespacecocos2d;
- usingnamespaceCocosDenshion;
- CCScene*HelloWorld::scene()
- {
- //'scene'isanautoreleaSEObject
- CCScene*scene=CCScene::create();
- //'layer'isanautoreleaSEObject
- HelloWorld*layer=HelloWorld::create();
- //addlayerasachildtoscene
- scene->addChild(layer);
- //returnthescene
- returnscene;
- }
- //on"init"youneedtoinitializeyourinstance
- boolHelloWorld::init()
- //////////////////////////////
- //1.superinitfirst
- if(!CCLayer::init())
- returnfalse;
- m_iLoadIdex=0;
- /////////////////////////////
- //2.addamenuitemwith"X"image,whichisclickedtoquittheprogram
- //youmaymodifyit.
- //adda"close"icontoexittheprogress.it'sanautoreleaSEObject
- CCMenuItemImage*pCloseItem=CCMenuItemImage::create(
- "CloseNormal.png",
- "CloseSelected.png",
- this,153); list-style:decimal-leading-zero outside; color:inherit; line-height:20px; margin:0px!important; padding:0px 3px 0px 10px!important"> menu_selector(HelloWorld::menuCloseCallback));
- pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-20,20));
- //createmenu,it'sanautoreleaSEObject
- CCMenu*pMenu=CCMenu::create(pCloseItem,NULL);
- pMenu->setPosition(CCPointZero);
- this->addChild(pMenu,1);
- /////////////////////////////
- //3.addyourcodesbelow...
- //addalabelshows"HelloWorld"
- //createandinitializealabel
- CCLabelTTF*pLabel=CCLabelTTF::create("HelloWorld","Thonburi",34);
- //askdirectorthewindowsize
- CCSizesize=CCDirector::sharedDirector()->getWinSize();
- //positionthelabelonthecenterofthescreen
- pLabel->setPosition(ccp(size.width/2,size.height-20));
- //addthelabelasachildtothislayer
- this->addChild(pLabel,0); background-color:inherit">//add"HelloWorld"splashscreen"
- CCSprite*pSprite=CCSprite::create("HelloWorld.png");
- //positionthespriteonthecenterofthescreen
- pSprite->setPosition(ccp(size.width/2,size.height/2));
- //addthespriteasachildtothislayer
- 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,153); list-style:decimal-leading-zero outside; color:inherit; line-height:20px; margin:0px!important; padding:0px 3px 0px 10px!important"> m_progressBar->setPercentage(0);
- //loading动画,没有逻辑处理,实际情况则注释掉
- CCProgressTo*to=CCProgressTo::create(10,100);
- m_progressBar->runAction(to);
- //实际的loading逻辑,可以在这里加入
- //scheduleUpdate();
- returntrue;
- voidHelloWorld::update(floatdt)
- {
- m_iLoadIdex++;
- if(m_iLoadIdex<=50){
- loadResource(m_iLoadIdex);
- m_progressBar->setPercentage(m_iLoadIdex*100.0/50);
- if(m_iLoadIdex>=50){
- unscheduleUpdate();
- }
- //实际的loading逻辑,加载资源可以在这里添加。
- //这里只用了sleep来模拟
- voidHelloWorld::loadResource(intindex)
- CCLog("loading....");
- switch(index){
- case0:
- break;
- default:
- sleep(1.0);
- break;
- voidHelloWorld::menuCloseCallback(CCObject*pSender)
- CCDirector::sharedDirector()->end();
- #if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
- exit(0);
- #endif
- }