cocos2d-x 弹入、弹出效果(以菜单为例子)

前端之家收集整理的这篇文章主要介绍了cocos2d-x 弹入、弹出效果(以菜单为例子)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1.  

弹入和弹出菜单为了使动作更平滑,涉及到动作组合。(CCMoveTo 、CCEaseExponentialOut)(菜单背景图位置仅为示范例子,还需调整)

菜单的背景图为例:

  1. //生成菜单背景图
  2. CCSprite* MainMenuBG = CCSprite::create("menu_bg.png");
  3. MainMenuBG->setPosition(ccp(visibleSize.width/2 +10,visibleSize.height +20));
  4. this->addChild(MainMenuBG,0);
  5. MainMenuBG->setTag(menu_bg_tag);
  6. //定义背景层 弹入弹出
  7. //MainMenuBG Moveout
  8. CCMoveTo* MainMenuBGMoveout = CCMoveTo::create(3.0f,ccp(visibleSize.width/2,visibleSize.height/2));
  9. CCEaseExponentialOut* MainMenuBGExponentialOut = CCEaseExponentialOut::create(MainMenuBGMoveout);
  10.  
  11. //MainMenuBG Movein
  12. MainMenuBG = (CCSprite*)this->getChildByTag(menu_bg_tag);
  13. CCPoint MainMenuBGHidepoint = ccp(visibleSize.width/2,visibleSize.height + MainMenuBG->getContentSize().height/2);
  14. CCMoveTo* MainMenuBGMovein=CCMoveTo::create(3.0f,MainMenuBGHidepoint);
  15. CCEaseExponentialIn* MainMenuBGExponentialIn = CCEaseExponentialIn::create(MainMenuBGMovein);
  16. //调用顺序复合动作
  17. CCRepeatForever* rept0= CCRepeatForever::create(CCSequence::create(MainMenuBGExponentialOut,MainMenuBGExponentialIn,NULL));
  18. MainMenuBG->runAction(rept0);


==================



源码如下:

  1. /////////////////////////////////////////////
  2. /*HelloWorldScene.h*/
  3. #ifndef __HELLOWORLD_SCENE_H__
  4. #define __HELLOWORLD_SCENE_H__
  5.  
  6.  
  7. #include "cocos2d.h"
  8.  
  9.  
  10. class HelloWorld : public cocos2d::CCLayer
  11. {
  12. public:
  13. // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'id' in cocos2d-iphone
  14. virtual bool init();
  15.  
  16.  
  17. // there's no 'id' in cpp,so we recommend returning the class instance pointer
  18. static cocos2d::CCScene* scene();
  19. // a selector callback
  20. void menuCloseCallback(CCObject* pSender);
  21.  
  22.  
  23. //定义菜单
  24. bool MainMenu();
  25.  
  26.  
  27. // implement the "static node()" method manually
  28. CREATE_FUNC(HelloWorld);
  29. };
  30.  
  31.  
  32. #endif // __HELLOWORLD_SCENE_H__
  33.  
  34. //////////////////////////////////////////////////////////////////
  35. /* HelloWorldScene.cpp */
  36. #include "HelloWorldScene.h"
  37.  
  38.  
  39. USING_NS_CC;
  40.  
  41.  
  42. enum{
  43. menu_pause_tag =1,menu_bg_tag=2
  44. };
  45.  
  46.  
  47. CCScene* HelloWorld::scene()
  48. {
  49. // 'scene' is an autorelease object
  50. CCScene *scene = CCScene::create();
  51. // 'layer' is an autorelease object
  52. HelloWorld *layer = HelloWorld::create();
  53.  
  54.  
  55. scene->addChild(layer,999);
  56.  
  57.  
  58.  
  59.  
  60. // return the scene
  61. return scene;
  62. }
  63.  
  64.  
  65. // on "init" you need to initialize your instance
  66. bool HelloWorld::init()
  67. {
  68. //////////////////////////////
  69. // 1. super init first
  70. if ( !CCLayer::init() )
  71. {
  72. return false;
  73. }
  74. CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
  75. CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
  76.  
  77.  
  78. /////////////////////////////
  79. // 2. add a menu item with "X" image,which is clicked to quit the program
  80. // you may modify it.
  81.  
  82.  
  83. // add a "close" icon to exit the progress. it's an autorelease object
  84. CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
  85. "CloseNormal.png","CloseSelected.png",this,menu_selector(HelloWorld::menuCloseCallback));
  86. pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2,origin.y + pCloseItem->getContentSize().height/2));
  87.  
  88.  
  89. // create menu,it's an autorelease object
  90. CCMenu* pMenu = CCMenu::create(pCloseItem,NULL);
  91. pMenu->setPosition(CCPointZero);
  92. this->addChild(pMenu,1);
  93.  
  94.  
  95. /////////////////////////////
  96. // 3. add your codes below...
  97.  
  98. //调用菜单
  99. MainMenu();
  100.  
  101. return true;
  102. }
  103.  
  104.  
  105.  
  106.  
  107. void HelloWorld::menuCloseCallback(CCObject* pSender)
  108. {
  109. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
  110. CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
  111. #else
  112. CCDirector::sharedDirector()->end();
  113. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  114. exit(0);
  115. #endif
  116. #endif
  117. }
  118.  
  119.  
  120. bool HelloWorld::MainMenu()
  121. {
  122. CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
  123. //
  124.  
  125. //设定菜单项 MenuItem
  126. CCMenuItemImage *item0 = CCMenuItemImage::create(
  127. "menu_restart_normal_CN.png","menu_restart_pressed_CN.png",menu_selector(HelloWorld::menuCloseCallback));
  128. //MenuItem 1
  129. CCMenuItemImage *item1 = CCMenuItemImage::create(
  130. "menu_resume_normal_CN.png","menu_resume_pressed_CN.png",menu_selector(HelloWorld::menuCloseCallback));
  131. //MenuItem 2
  132. CCMenuItemImage *item2 = CCMenuItemImage::create(
  133. "menu_quit_normal_CN.png","menu_quit_pressed_CN.png",menu_selector(HelloWorld::menuCloseCallback));
  134. //MenuItem 3
  135. CCMenuItemImage *item3 = CCMenuItemImage::create(
  136. "CloseNormal.png",menu_selector(HelloWorld::menuCloseCallback));
  137. //根据菜单生成菜单
  138. CCMenu *MainMenu = CCMenu::create(item0,item1,item2,item3,NULL);
  139. MainMenu->alignItemsVerticallyWithPadding(15.0f);
  140. MainMenu->setPosition(ccp(visibleSize.width/2,visibleSize.height));
  141.  
  142.  
  143. MainMenu->setTag(menu_pause_tag);
  144. this->addChild(MainMenu,1);
  145.  
  146.  
  147. //生成菜单背景图
  148. CCSprite* MainMenuBG = CCSprite::create("menu_bg.png");
  149. MainMenuBG->setPosition(ccp(visibleSize.width/2 +10,0);
  150. MainMenuBG->setTag(menu_bg_tag);
  151.  
  152.  
  153. //定义弹出 菜单弹入弹出效果
  154. //MainMenu moveout
  155. CCMoveTo* MainMenuMoveOut = CCMoveTo::create(3.0f,visibleSize.height/2));
  156.  
  157.  
  158. CCEaseExponentialOut* MainMenuExponentialOut = CCEaseExponentialOut::create(MainMenuMoveOut);
  159.  
  160. //MainMenu movein
  161. MainMenu = (CCMenu*)this->getChildByTag(menu_pause_tag);
  162. CCPoint MainMenuHidepoint = ccp(visibleSize.width/2,visibleSize.height + MainMenu->getContentSize().height/2);
  163. CCMoveTo* MainMenuMovein=CCMoveTo::create(3.0f,MainMenuHidepoint);
  164. CCEaseExponentialIn* MainMenuExponentailIn = CCEaseExponentialIn::create(MainMenuMovein);
  165.  
  166.  
  167. //
  168. //定义背景层 弹入弹出
  169. //MainMenuBG Moveout
  170. CCMoveTo* MainMenuBGMoveout = CCMoveTo::create(3.0f,visibleSize.height/2));
  171.  
  172.  
  173. CCEaseExponentialOut* MainMenuBGExponentialOut = CCEaseExponentialOut::create(MainMenuBGMoveout);
  174.  
  175. //MainMenuBG Movein
  176. MainMenuBG = (CCSprite*)this->getChildByTag(menu_bg_tag);
  177. CCPoint MainMenuBGHidepoint = ccp(visibleSize.width/2,MainMenuBGHidepoint);
  178. CCEaseExponentialIn* MainMenuBGExponentialIn = CCEaseExponentialIn::create(MainMenuBGMovein);
  179.  
  180.  
  181.  
  182.  
  183. //调用弹入 弹出动作
  184.  
  185. CCRepeatForever* rept1= CCRepeatForever::create(CCSequence::create(MainMenuExponentialOut,MainMenuExponentailIn,NULL));
  186. MainMenu->runAction(rept1);
  187.  
  188.  
  189. CCRepeatForever* rept0= CCRepeatForever::create(CCSequence::create(MainMenuBGExponentialOut,NULL));
  190. MainMenuBG->runAction(rept0);
  191.  
  192.  
  193.  
  194. return true;
  195. }

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