基于cocos2dx的伪立体菜单

前端之家收集整理的这篇文章主要介绍了基于cocos2dx的伪立体菜单前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

RotateMenu说明

最终效果


下面这个引用自乱斗西游

设计说明

1.菜单项(MenuItem)平均分布在椭圆(类似)上

2.椭圆长轴为2/3width,短轴为2/8height

3.最前面的菜单项Scale=1,opacity=255,最后面Scale=0.5,opacity=129.其它位置根据三角函数变换(updatePosition中实现)

4.默认最前面菜单被选中(selected)

5.单位角度(unitAngle)2*PI/菜单项的数量

6.滑动一个width菜单旋转两个单位角度

7.Touch结束会自动调整位置,保证最前面位置有菜单

8.滑动超过1/3单位角度会向前舍入

9.移动小于1/6单位角度会判定点击菜单

10.默认菜单大小不是全屏,而是屏幕的2/3,通过Node::setContentSize()设置

使用

使用这个菜单只要知道两个函数

1.构造函数

RotateMenu::create()(由CREATE_FUNC创建)

2.添加MenuItem

voidaddMenuItem(cocos2d::MenuItem*item);

其它函数可以看代码

相关参数的函数设置还未添加

代码

声明

[cpp] @L_403_4@ copy
  1. #ifndef__ROTA__TE_MENU_H__
  2. #define__ROTA__TE_MENU_H__
  3. #include"cocos2d.h"
  4. /*
  5. *模仿乱斗西游主界面的旋转菜单
  6. */
  7. classRotateMenu:publiccocos2d::Layer{
  8. public:
  9. //构造方法
  10. CREATE_FUNC(RotateMenu);
  11. //添加菜单
  12. voidaddMenuItem(cocos2d::MenuItem*item);
  13. //更新位置
  14. voidupdatePosition();
  15. //更新位置,有动画
  16. voidupdatePositionWithAnimation();
  17. //位置矫正修改角度forward为移动方向当超过1/3,进1
  18. //true为正向false负
  19. voidrectify(boolforward);
  20. //初始化
  21. virtualboolinit();
  22. //重置操作有旋转角度设为0
  23. voidreset();
  24. private:
  25. //设置角度弧度
  26. voidsetAngle(floatangle);
  27. floatgetAngle();
  28. //设置单位角度弧度
  29. voidsetUnitAngle(floatangle);
  30. floatgetUnitAngle();
  31. //滑动距离转换角度,转换策略为移动半个Menu.width等于_unitAngle
  32. floatdisToAngle(floatdis);
  33. //返回被选中的item
  34. cocos2d::MenuItem*getCurrentItem();
  35. private:
  36. //菜单已经旋转角度弧度
  37. float_angle;
  38. //菜单项集合,_children顺序会变化,新建数组保存顺序
  39. cocos2d::Vector<cocos2d::MenuItem*>_items;
  40. //单位角度弧度
  41. float_unitAngle;
  42. //监听函数
  43. boolonTouchBegan(cocos2d::Touch*touch,cocos2d::Event*event);
  44. virtualvoidonTouchEnded(cocos2d::Touch*touch,cocos2d::Event*event);
  45. voidonTouchMoved(cocos2d::Touch*touch,0); background-color:inherit">//动画完结调用函数
  46. voidactionEndCallBack(floatdx);
  47. //当前被选择的item
  48. cocos2d::MenuItem*_selectedItem;
  49. //动画运行时间
  50. floatanimationDuration=0.3f;
  51. };
  52. #endif

实现

@H_865_502@copy
    #include"RotateMenu.h"
  1. #include<math.h>
  2. #definePIacos(-1)
  3. USING_NS_CC;
  4. boolRotateMenu::init(){
  5. if(!Layer::init())
  6. returnfalse;
  7. _angle=0.0;
  8. this->ignoreAnchorPointForPosition(false);
  9. _selectedItem=nullptr;
  10. Sizes=Director::getInstance()->getWinSize();
  11. this->setContentSize(s/3*2);
  12. this->setAnchorPoint(Vec2(0.5f,0.5f));
  13. autolistener=EventListenerTouchOneByOne::create();
  14. listener->onTouchBegan=CC_CALLBACK_2(RotateMenu::onTouchBegan,this);
  15. listener->onTouchMoved=CC_CALLBACK_2(RotateMenu::onTouchMoved,153); font-weight:bold; background-color:inherit">this);
  16. listener->onTouchEnded=CC_CALLBACK_2(RotateMenu::onTouchEnded,248)"> getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,153); font-weight:bold; background-color:inherit">true;
  17. voidRotateMenu::addMenuItem(cocos2d::MenuItem*item){
  18. item->setPosition(this->getContentSize()/2);
  19. this->addChild(item);
  20. _items.pushBack(item);
  21. setUnitAngle(2*PI/_items.size());
  22. reset();
  23. updatePositionWithAnimation();
  24. return;
  25. }
  26. voidRotateMenu::updatePosition(){
  27. automenuSize=getContentSize();
  28. autodisY=menuSize.height/8;
  29. autodisX=menuSize.width/3;
  30. for(inti=0;i<_items.size();i++){
  31. floatx=menuSize.width/2+disX*sin(i*_unitAngle+getAngle());
  32. floaty=menuSize.height/2-disY*cos(i*_unitAngle+getAngle());
  33. _items.at(i)->setPosition(Vec2(x,y));
  34. _items.at(i)->setZOrder(-(int)y);
  35. //Opacity129~255
  36. _items.at(i)->setOpacity(192+63*cos(i*_unitAngle+getAngle()));
  37. _items.at(i)->setScale(0.75+0.25*cos(i*_unitAngle+getAngle()));
  38. }
  39. return;
  40. voidRotateMenu::updatePositionWithAnimation(){
  41. //先停止所有可能存在的动作
  42. inti=0;i<_items.size();i++)
  43. _items.at(i)->stopAllActions();
  44. automoveTo=MoveTo::create(animationDuration,Vec2(x,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> _items.at(i)->runAction(moveTo);
  45. autofadeTo=FadeTo::create(animationDuration,(192+63*cos(i*_unitAngle+getAngle())));
  46. _items.at(i)->runAction(fadeTo);
  47. //缩放比例0.5~1
  48. autoscaleTo=ScaleTo::create(animationDuration,0.75+0.25*cos(i*_unitAngle+getAngle()));
  49. _items.at(i)->runAction(scaleTo);
  50. _items.at(i)->setZOrder(-(int)y);
  51. scheduleOnce(schedule_selector(RotateMenu::actionEndCallBack),animationDuration);
  52. voidRotateMenu::reset(){
  53. _angle=0;
  54. voidRotateMenu::setAngle(floatangle){
  55. this->_angle=angle;
  56. floatRotateMenu::getAngle(){
  57. return_angle;
  58. voidRotateMenu::setUnitAngle( _unitAngle=angle;
  59. floatRotateMenu::getUnitAngle(){
  60. return_unitAngle;
  61. floatRotateMenu::disToAngle(floatdis){
  62. floatwidth=this->getContentSize().width/2;
  63. returndis/width*getUnitAngle();
  64. MenuItem*RotateMenu::getCurrentItem(){
  65. if(_items.size()==0)
  66. returnnullptr;
  67. //这里实际加上了0.1getAngle(),用来防止精度丢失
  68. intindex=(int)((2*PI-getAngle())/getUnitAngle()+0.1*getUnitAngle());
  69. index%=_items.size();
  70. return_items.at(index);
  71. boolRotateMenu::onTouchBegan(Touch*touch,Event*event){
  72. if(_selectedItem)
  73. _selectedItem->unselected();
  74. autoposition=this->convertToNodeSpace(touch->getLocation());
  75. autosize=this->getContentSize();
  76. autorect=Rect(0,size.width,size.height);
  77. if(rect.containsPoint(position)){
  78. true;
  79. false;
  80. voidRotateMenu::onTouchEnded(Touch*touch,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> autoxDelta=touch->getLocation().x-touch->getStartLocation().x;
  81. rectify(xDelta>0);
  82. if(disToAngle(fabs(xDelta))<getUnitAngle()/6&&_selectedItem)
  83. _selectedItem->activate();
  84. updatePositionWithAnimation();
  85. voidRotateMenu::onTouchMoved(Touch*touch,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> autoangle=disToAngle(touch->getDelta().x);
  86. setAngle(getAngle()+angle);
  87. updatePosition();
  88. voidRotateMenu::rectify(boolforward){
  89. autoangle=getAngle();
  90. while(angle<0)
  91. angle+=PI*2;
  92. while(angle>PI*2)
  93. angle-=PI*2;
  94. if(forward>0)
  95. angle=((int)((angle+getUnitAngle()/3*2)/getUnitAngle()))*getUnitAngle();
  96. else
  97. int)((angle+getUnitAngle()/3)/getUnitAngle()))*getUnitAngle();
  98. setAngle(angle);
  99. voidRotateMenu::actionEndCallBack(floatdx){
  100. _selectedItem=getCurrentItem();
  101. _selectedItem->selected();
  102. }

一个糟糕的Demo

声明

@H_865_502@copy
    #ifndef__HELLOWORLD_SCENE_H__
  1. #define__HELLOWORLD_SCENE_H__
  2. classHelloWorld:publiccocos2d::Layer
  3. {
  4. //there'sno'id'incpp,sowerecommendreturningtheclassinstancepointer
  5. staticcocos2d::Scene*createScene();
  6. //Here'sadifference.Method'init'incocos2d-xreturnsbool,insteadofreturning'id'incocos2d-iphone
  7. boolinit();
  8. //aselectorcallback
  9. voidmenuCloseCallback(cocos2d::Ref*pSender);
  10. voidmenuItem1Callback(cocos2d::Ref*pSender);
  11. voidmenuItem2Callback(cocos2d::Ref*pSender);
  12. voidmenuItem3Callback(cocos2d::Ref*pSender);
  13. voidmenuItem4Callback(cocos2d::Ref*pSender);
  14. voidmenuItem5Callback(cocos2d::Ref*pSender);
  15. voidhideAllSprite();
  16. cocos2d::Sprite*sprite[5];
  17. //implementthe"staticcreate()"methodmanually
  18. CREATE_FUNC(HelloWorld);
  19. #endif//__HELLOWORLD_SCENE_H__

声明

@H_865_502@copy
    #include"HelloWorldScene.h"
  1. #include"RotateMenu.h"
  2. USING_NS_CC;
  3. typedefstructSceneList{
  4. constchar*name;
  5. std::function<cocos2d::Scene*()>callback;
  6. }SceneList;
  7. SceneListsceneList[]={
  8. {"Demo1",[](){returnHelloWorld::createScene();}}
  9. constunsignedintsceneCount=sizeof(sceneList)/sizeof(SceneList);
  10. #defineLINE_SPACE40
  11. Scene*HelloWorld::createScene()
  12. //'scene'isanautoreleaSEObject
  13. autoscene=Scene::create();
  14. //'layer'isanautoreleaSEObject
  15. autolayer=HelloWorld::create();
  16. //addlayerasachildtoscene
  17. scene->addChild(layer);
  18. //returnthescene
  19. returnscene;
  20. //on"init"youneedtoinitializeyourinstance
  21. boolHelloWorld::init()
  22. //////////////////////////////
  23. //1.superinitfirst
  24. SizevisibleSize=Director::getInstance()->getVisibleSize();
  25. Vec2origin=Director::getInstance()->getVisibleOrigin();
  26. /////////////////////////////
  27. //2.addamenuitemwith"X"image,whichisclickedtoquittheprogram
  28. //youmaymodifyit.
  29. //adda"close"icontoexittheprogress.it'sanautoreleaSEObject
  30. autocloseItem=MenuItemImage::create(
  31. "CloseNormal.png",
  32. "CloseSelected.png",
  33. CC_CALLBACK_1(HelloWorld::menuCloseCallback,153); font-weight:bold; background-color:inherit">this));
  34. closeItem->setPosition(Vec2(origin.x+visibleSize.width-closeItem->getContentSize().width/2,
  35. origin.y+closeItem->getContentSize().height/2));
  36. //createmenu,it'sanautoreleaSEObject
  37. /*automenu=Menu::create(closeItem,NULL);
  38. menu->setPosition(Vec2::ZERO);
  39. this->addChild(menu,1);*/
  40. autoitem1=MenuItemImage::create("Demo1/item1_1.png","Demo1/item1_0.png",CC_CALLBACK_1(HelloWorld::menuItem1Callback,this));
  41. autoitem2=MenuItemImage::create("Demo1/item2_1.png","Demo1/item2_0.png",CC_CALLBACK_1(HelloWorld::menuItem2Callback,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> autoitem3=MenuItemImage::create("Demo1/item3_1.png","Demo1/item3_0.png",CC_CALLBACK_1(HelloWorld::menuItem3Callback,248)"> autoitem4=MenuItemImage::create("Demo1/item4_1.png","Demo1/item4_0.png",CC_CALLBACK_1(HelloWorld::menuItem4Callback,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> autoitem5=MenuItemImage::create("Demo1/item5_1.png","Demo1/item5_0.png",CC_CALLBACK_1(HelloWorld::menuItem5Callback,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> RotateMenu*menu=RotateMenu::create();
  42. menu->addMenuItem(item1);
  43. menu->addMenuItem(item2);
  44. menu->addMenuItem(item3);
  45. menu->addMenuItem(item4);
  46. menu->addMenuItem(item5);
  47. menu->setPosition(visibleSize/2);
  48. this->addChild(menu,2);
  49. inti=0;i<5;i++){
  50. charstr[20];
  51. sprintf(str,"Demo1/item%d.jpg",i+1);
  52. sprite[i]=Sprite::create(str);
  53. sprite[i]->setAnchorPoint(Vec2(0.5f,0.5f));
  54. sprite[i]->setPosition(visibleSize/2);
  55. this->addChild(sprite[i]);
  56. hideAllSprite();
  57. /////////////////////////////
  58. //3.addyourcodesbelow...
  59. //addalabelshows"HelloWorld"
  60. //createandinitializealabel
  61. autolabel=LabelTTF::create("HelloWorld","Arial",24);
  62. //positionthelabelonthecenterofthescreen
  63. label->setPosition(Vec2(origin.x+visibleSize.width/2,
  64. origin.y+visibleSize.height-label->getContentSize().height));
  65. //addthelabelasachildtothislayer
  66. this->addChild(label,1);
  67. voidHelloWorld::menuCloseCallback(Ref*pSender)
  68. #if(CC_TARGET_PLATFORM==CC_PLATFORM_WP8)||(CC_TARGET_PLATFORM==CC_PLATFORM_WINRT)
  69. MessageBox("Youpressedtheclosebutton.WindowsStoreAppsdonotimplementaclosebutton.","Alert");
  70. #endif
  71. Director::getInstance()->end();
  72. #if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
  73. exit(0);
  74. voidHelloWorld::menuItem1Callback(cocos2d::Ref*pSender){
  75. sprite[0]->setVisible(true);
  76. voidHelloWorld::menuItem2Callback(cocos2d::Ref*pSender){
  77. sprite[1]->setVisible(voidHelloWorld::menuItem3Callback(cocos2d::Ref*pSender){
  78. sprite[2]->setVisible(voidHelloWorld::menuItem4Callback(cocos2d::Ref*pSender){
  79. sprite[3]->setVisible(voidHelloWorld::menuItem5Callback(cocos2d::Ref*pSender){
  80. sprite[4]->setVisible(voidHelloWorld::hideAllSprite(){
  81. for(autop:sprite){
  82. if(p->isVisible())
  83. p->setVisible(false);
  84. }

可运行的程序下载地址 原文链接:https://www.f2er.com/cocos2dx/339218.html

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