Cocos2d-X中Menu的综合运用

前端之家收集整理的这篇文章主要介绍了Cocos2d-X中Menu的综合运用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

今天将以前写的代码和项目集成到了一个菜单中,可以通过菜单切换到以前做的项目

程序的工程目录


主要代码分析:

LessonMenu.h中实现创建菜单,遍历菜单通过菜单切换到各个项目

#ifndef _LessonMenu_H_
#define _LessonMenu_H_

#include "cocos2d.h"
USING_NS_CC;

class LessonMenu : public CCLayer
{
public:
    bool init();  

    static CCScene* scene();

    void EnterLasson(CCObject* sender);

    //处理触摸事件
     bool ccTouchBegan(CCTouch*,CCEvent);  
     void ccTouchMoved(CCTouch*,CCEvent*);  

    CREATE_FUNC(LessonMenu);
};

#endif

LessonMenu.cpp中实现具体的功能

#include "LessonMenu.h"
#include"Label.h"       
#include"MenuItem.h"     
#include"TextFieldTTF.h"
#include"AppMacros.h"        
#include"LabelAtlas.h"  
#include"SceneSecond.h"  
#include"TextFieldTTF1.h"
#include"BMFont.h"           
#include"Layer.h"       
#include"Sprite.h"       
#include"Zorder.h"
#include"Coord.h"            
#include"Sprite1.h"
#include"TCCEditBox.h"
#include"Menu.h"      
#include "MenuContinus.h"
#include "T14SwitchControl.h"
#include "T15Slider.h"

CCScene* LessonMenu::scene()
{
    CCScene* s = CCScene::create();

    LessonMenu* layer = LessonMenu::create();

    s->addChild(layer);

    return s;
}

//保存创建的cpp文件
static const char* lessons[] = 
{
 "BMFont.cpp","Coord.cpp","Label.cpp","LabelAtlas.cpp","Layer.cpp","Menu.cpp","MenuItem.cpp","SceneSecond.cpp","Sprite.cpp","Sprite1.cpp","TCCEditBox.cpp","TextFieldTTF.cpp","TextFieldTTF1.cpp","Zorder.cpp","MenuContinus.cpp","T14SwitchControl.cpp","T15Slider.cpp"
};

bool LessonMenu::init()
{
    CCLayer::create();

    //创建菜单
    CCMenu* menu = CCMenu::create();
    addChild(menu);

    for(int i = 0; i < sizeof(lessons)/sizeof(*lessons); i++)
    {
        //创建菜单项
        CCMenuItemFont* item = CCMenuItemFont::create(lessons[i]);
        menu->addChild(item);

        //设置处理函数
        item->setTarget(this,menu_selector(LessonMenu::EnterLasson));

        //设置菜单项的ID
        item->setTag(10000 + i);
    }

    //对齐菜单项
    menu->alignItemsVertically();

    //触摸菜单项
     setTouchEnabled(true);
	setTouchMode(kCCTouchesOneByOne);

    return true;
}

void LessonMenu::EnterLasson(CCObject* sender)
{
    //强制类型转换
    CCMenuItem* item = dynamic_cast<CCMenuItem*>(sender);

    //得到菜单项的ID
    int tag = item->getTag() - 10000;

    //用于调用场景
    CCScene* scene = NULL;

    switch(tag)
    {
    case 0:
        {
            scene = BMFont::scene();
        }
        break;

    case 1:
        {
            scene = Coord::scene();
        }
        break;
    
    case 2:
        {
            scene = Label::scene();
        }
        break;
    
    case 3:
        {
            scene = LabelAtlas::scene();
        }
        break;
    
    case 4:
        {
            scene = Layer::scene();
        }
        break;
    
    case 5:
        {
            scene = Menu::scene();
        }
        break;
    
    case 6:
        {
            scene = MenuItem::scene();
        }
        break;
    
    case 7:
        {
            scene = SceneSecond::scene();
        }
        break;
    
    case 8:
       {
           scene = Sprite::scene();
        }
        break;

   case 9:
       {
           scene = Sprite1::scene();
        }
        break;
   
   case 10:
       {
           scene = TCCEditBox::scene();
        }
        break;

   case 11:
       {
           scene = TextFieldTTF::scene();
       }
       break;

   case 12:
       {
           scene = TextFieldTTF1::scene();
       }
       break;

   case 13:
       {
           scene = Zorder::scene();
       }
       break;

   case 14:
       {
           scene = MenuContinus::scene();
       }
       break;

   case 15:
       {
           scene = T14SwitchControl::scene();
       }
       break;

   case 16:
       {
           scene = T15Slider::scene();
       }
       break;
    }

    if(scene)
    {
        CCDirector::sharedDirector()->pushScene(scene);
    }
}

//处理触摸事件
bool LessonMenu::ccTouchBegan(CCTouch* touch,CCEvent)
{
    return true;
}

//处理触摸事件
void LessonMenu::ccTouchMoved(CCTouch* touch,CCEvent*)
{
	// 获得触摸移动的距
	CCPoint ptDelta = touch->getDelta();

    // 只上下滑动的方法
	setPositionY(getPositionY() + ptDelta.y);
}

LayerBack.h实现创建一个只有一个返回按钮的层,通过返回按钮实现场景切换

#ifndef _LayerBack_H_
#define _LayerBack_H_

#include "cocos2d.h"
USING_NS_CC;

class LayerBack : public CCLayer
{
public:
    bool init();

    void back(CCObject*);
};

#endif


LayerBack.cpp中是具体实现的代码

#include "LayerBack.h"

bool LayerBack::init()
{
    CCLayer::init();

    //得到窗口的尺寸
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    CCMenu* menu = CCMenu::create();
    addChild(menu);

    CCMenuItem* backItem = CCMenuItemImage::create("CloseNormal.png","CloseSelected.png",this,menu_selector(LayerBack::back));

    menu->addChild(backItem);

    //设置菜单项的位置(将菜单项设置到右下角)
    backItem->setPosition(ccp(winSize.width / 2 - backItem->getContentSize().width / 2,backItem->getContentSize().height / 2 - winSize.height / 2));

    return true;
}

void LayerBack::back(CCObject*)
{
    //场景切换
    CCDirector::sharedDirector()->popScene();
}

执行结果:



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