cocos2d-x 3.9进度条程序实例(圆形)

前端之家收集整理的这篇文章主要介绍了cocos2d-x 3.9进度条程序实例(圆形)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

.h

  1. #ifndef __HELLOWORLD_SCENE_H__
  2. #define __HELLOWORLD_SCENE_H__
  3.  
  4. #include "cocos2d.h"
  5.  
  6. USING_NS_CC;
  7.  
  8. class HelloWorld : public cocos2d::Layer
  9. {
  10. public:
  11. static cocos2d::Scene* createScene();
  12.  
  13. virtual bool init();
  14.  
  15. // a selector callback
  16. void menuCloseCallback(cocos2d::Ref* pSender);
  17.  
  18. // implement the "static create()" method manually
  19. CREATE_FUNC(HelloWorld);
  20.  
  21.  
  22. void startProgress();
  23. void updateFireProgress(float dt);
  24.  
  25. ProgressTimer* fireballTimer;
  26. };
  27.  
  28. #endif // __HELLOWORLD_SCENE_H__

.CPP

  1. #include "HelloWorldScene.h"
  2.  
  3. USING_NS_CC;
  4.  
  5. Scene* HelloWorld::createScene()
  6. {
  7. // 'scene' is an autorelease object
  8. auto scene = Scene::create();
  9.  
  10. // 'layer' is an autorelease object
  11. auto layer = HelloWorld::create();
  12.  
  13. // add layer as a child to scene
  14. scene->addChild(layer);
  15.  
  16. // return the scene
  17. return scene;
  18. }
  19.  
  20. // on "init" you need to initialize your instance
  21. bool HelloWorld::init()
  22. {
  23. //////////////////////////////
  24. // 1. super init first
  25. if ( !Layer::init() )
  26. {
  27. return false;
  28. }
  29.  
  30. Size visibleSize = Director::getInstance()->getVisibleSize();
  31. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  32.  
  33. /////////////////////////////
  34. // 2. add a menu item with "X" image,which is clicked to quit the program
  35. // you may modify it.
  36.  
  37. // add a "close" icon to exit the progress. it's an autorelease object
  38. auto closeItem = MenuItemImage::create(
  39. "CloseNormal.png","CloseSelected.png",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
  40.  
  41. closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2,origin.y + closeItem->getContentSize().height/2));
  42.  
  43. // create menu,it's an autorelease object
  44. auto menu = Menu::create(closeItem,NULL);
  45. menu->setPosition(Vec2::ZERO);
  46. this->addChild(menu,1);
  47.  
  48. /////////////////////////////
  49. // 3. add your codes below...
  50.  
  51. // add a label shows "Hello World"
  52. // create and initialize a label
  53.  
  54. auto label = Label::createWithTTF("Hello World","fonts/Marker Felt.ttf",24);
  55.  
  56. // position the label on the center of the screen
  57. label->setPosition(Vec2(origin.x + visibleSize.width/2,origin.y + visibleSize.height - label->getContentSize().height));
  58.  
  59. // add the label as a child to this layer
  60. this->addChild(label,1);
  61. label->setVisible(false);
  62.  
  63. // add "HelloWorld" splash screen"
  64. // auto sprite = Sprite::create("HelloWorld.png");
  65.  
  66. // position the sprite on the center of the screen
  67. // sprite->setPosition(Vec2(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y));
  68.  
  69. // add the sprite as a child to this layer
  70. //this->addChild(sprite,0);
  71.  
  72. SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ingame_gui-hd.plist");
  73. Sprite* Hero1Sprite = Sprite::createWithSpriteFrameName("power_portrait_fireball_0001.png");
  74. Hero1Sprite->setPosition(Point(100,100));
  75. this->addChild(Hero1Sprite,1);
  76.  
  77. //建立Progresstime 环形进度条
  78. //Sprite* loadingSprite = Sprite::createWithSpriteFrameName("power_loading.png");
  79. //ProgressTimer* fireballTimer;
  80. fireballTimer = ProgressTimer::create(Sprite::createWithSpriteFrameName("power_loading.png"));
  81. fireballTimer->setPosition(Vec2(100,100));
  82. fireballTimer->setType(ProgressTimer::Type::RADIAL);
  83. fireballTimer->setPercentage(100);
  84. fireballTimer->setReverseDirection(true);
  85. this->addChild(fireballTimer,10);
  86. HelloWorld::startProgress();
  87.  
  88.  
  89. return true;
  90. }
  91.  
  92.  
  93. void HelloWorld::menuCloseCallback(Ref* pSender)
  94. {
  95. Director::getInstance()->end();
  96.  
  97. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  98. exit(0);
  99. #endif
  100. }
  101.  
  102.  
  103. void HelloWorld::startProgress()
  104. {
  105. this->schedule(schedule_selector(HelloWorld::updateFireProgress));
  106. }
  107.  
  108.  
  109.  
  110. void HelloWorld::updateFireProgress(float dt)
  111. {
  112. fireballTimer->setPercentage(fireballTimer->getPercentage() - dt * 5);
  113. if ((fireballTimer->getPercentage()) == 0)
  114. {
  115. this->unschedule(schedule_selector(HelloWorld::updateFireProgress));
  116. }
  117. }

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