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

前端之家收集整理的这篇文章主要介绍了cocos2d-x游戏开发(十五)游戏加载动画loading界面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

个人原创,欢迎转载:http://www.jb51.cc/article/p-xryqknmf-zw.html


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

上图:


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

代码

  1. #include"HelloWorldScene.h"
  2. #include"SimpleAudioEngine.h"
  3. usingnamespacecocos2d;
  4. usingnamespaceCocosDenshion;
  5. CCScene*HelloWorld::scene()
  6. {
  7. //'scene'isanautoreleaSEObject
  8. CCScene*scene=CCScene::create();
  9. //'layer'isanautoreleaSEObject
  10. HelloWorld*layer=HelloWorld::create();
  11. //addlayerasachildtoscene
  12. scene->addChild(layer);
  13. //returnthescene
  14. returnscene;
  15. }
  16. //on"init"youneedtoinitializeyourinstance
  17. boolHelloWorld::init()
  18. //////////////////////////////
  19. //1.superinitfirst
  20. if(!CCLayer::init())
  21. returnfalse;
  22. m_iLoadIdex=0;
  23. /////////////////////////////
  24. //2.addamenuitemwith"X"image,whichisclickedtoquittheprogram
  25. //youmaymodifyit.
  26. //adda"close"icontoexittheprogress.it'sanautoreleaSEObject
  27. CCMenuItemImage*pCloseItem=CCMenuItemImage::create(
  28. "CloseNormal.png",
  29. "CloseSelected.png",
  30. 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));
  31. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-20,20));
  32. //createmenu,it'sanautoreleaSEObject
  33. CCMenu*pMenu=CCMenu::create(pCloseItem,NULL);
  34. pMenu->setPosition(CCPointZero);
  35. this->addChild(pMenu,1);
  36. /////////////////////////////
  37. //3.addyourcodesbelow...
  38. //addalabelshows"HelloWorld"
  39. //createandinitializealabel
  40. CCLabelTTF*pLabel=CCLabelTTF::create("HelloWorld","Thonburi",34);
  41. //askdirectorthewindowsize
  42. CCSizesize=CCDirector::sharedDirector()->getWinSize();
  43. //positionthelabelonthecenterofthescreen
  44. pLabel->setPosition(ccp(size.width/2,size.height-20));
  45. //addthelabelasachildtothislayer
  46. this->addChild(pLabel,0); background-color:inherit">//add"HelloWorld"splashscreen"
  47. CCSprite*pSprite=CCSprite::create("HelloWorld.png");
  48. //positionthespriteonthecenterofthescreen
  49. pSprite->setPosition(ccp(size.width/2,size.height/2));
  50. //addthespriteasachildtothislayer
  51. this->addChild(pSprite,0);
  52. //loading边框
  53. m_progressFrame=CCSprite::create("loading_fr.png");
  54. addChild(m_progressFrame,1);
  55. m_progressFrame->setPosition(ccp(240,50));
  56. //loading的动作条
  57. m_progressBar=CCProgressTimer::create(CCSprite::create("loading_bar.png"));
  58. m_progressBar->setType(kCCProgressTimerTypeBar);
  59. addChild(m_progressBar);
  60. m_progressBar->setVisible(true);
  61. m_progressBar->setPosition(ccp(241,51));
  62. //进度动画运动方向,从左到右
  63. m_progressBar->setMidpoint(ccp(0,0));
  64. //宽高变化,这里是宽度变化
  65. 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);
  66. //loading动画,没有逻辑处理,实际情况则注释掉
  67. CCProgressTo*to=CCProgressTo::create(10,100);
  68. m_progressBar->runAction(to);
  69. //实际的loading逻辑,可以在这里加入
  70. //scheduleUpdate();
  71. returntrue;
  72. voidHelloWorld::update(floatdt)
  73. {
  74. m_iLoadIdex++;
  75. if(m_iLoadIdex<=50){
  76. loadResource(m_iLoadIdex);
  77. m_progressBar->setPercentage(m_iLoadIdex*100.0/50);
  78. if(m_iLoadIdex>=50){
  79. unscheduleUpdate();
  80. }
  81. //实际的loading逻辑,加载资源可以在这里添加
  82. //这里只用了sleep来模拟
  83. voidHelloWorld::loadResource(intindex)
  84. CCLog("loading....");
  85. switch(index){
  86. case0:
  87. break;
  88. default:
  89. sleep(1.0);
  90. break;
  91. voidHelloWorld::menuCloseCallback(CCObject*pSender)
  92. CCDirector::sharedDirector()->end();
  93. #if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
  94. exit(0);
  95. #endif
  96. }


东西太简单,还需要上传demo源码么。要的留言

猜你在找的Cocos2d-x相关文章