首页 > 代码库 > Cocosd-x3.1 Menu使用示例

Cocosd-x3.1 Menu使用示例

1、头文件

#include "cocos2d.h"
USING_NS_CC;

class MenuItemDemo : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();
    
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();
    
    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);
    
    // implement the "static create()" method manually
    CREATE_FUNC(MenuItemDemo);
};

2、cpp文件

#include "MenuItemDemo.h"
USING_NS_CC;

Scene* MenuItemDemo::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = MenuItemDemo::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
    
}

bool MenuItemDemo::init()
{
    bool bRet = false;
    do{
        CC_BREAK_IF(!Layer::init());
        auto winSize = Director::getInstance()->getWinSize();
        //文字菜单项
        auto item1 = MenuItemFont::create("Item Font Test",[&](Ref* sender)
                                          {
                                              log("Item Font CallBack called");
                                          });
        item1->setFontSize(40);
        item1->setFontName("Marker Felt");
        item1->setPosition(Vec2(winSize.width/4,winSize.height/3*2));
        
        auto menu1 = Menu::create(item1,NULL);
        menu1->setPosition(Vec2::ZERO);
        addChild(menu1);
        
        //图片菜单项
        auto item2 = MenuItemImage::create("CloseNormal.png","CloseSelected.png",[&](Ref* sender){
            log("MenuItemImage");
        });
        item2->setPosition(Vec2(winSize.width/2,winSize.height/3*2));
        menu1->addChild(item2);
        //标签菜单项
        auto item3 = MenuItemLabel::create(LabelTTF::create("Label Test", "Arial", 40), [&](Ref* sender){
            log("Label CallBack called");
        });
        item3->setPosition(Vec2(winSize.width/4*3,winSize.height/3*2));
        menu1->addChild(item3);
        //触发器菜单项,实现开关关闭效果
        auto item4 = MenuItemToggle::createWithCallback([&](Ref* sender){
            log("Toggle Touched");
        }, MenuItemFont::create("On"),MenuItemFont::create("Off"),NULL);
        MenuItemFont::setFontName("Marker Felt");
        MenuItemFont::setFontSize(35);
        item4->setPosition(Vec2(winSize.width/4,winSize.height/3));
        menu1->addChild(item4);
        //高亮效果
        auto item5 = MenuItemFont::create("Notice This Menu",[&](Ref* sender){
            log("MenuItem touched");
        });
        item5->setPosition(Vec2(winSize.width/2,winSize.height/3));
        auto color_action = TintBy::create(0.5f, 0, -255, -255);
        auto color_back = color_action->reverse();
        auto seq = Sequence::create(color_action,color_back, NULL);
        item5->runAction(RepeatForever::create(seq));
        menu1->addChild(item5);
        //移动菜单
        auto item6 = MenuItemFont::create("Move MenuItem",[&](Ref*){
            log("Move Item Called");
        });
        item6->setPosition(Vec2(winSize.width/4*3,winSize.height/3));
        auto moveTo = MoveTo::create(2.25, Vec2(winSize.width/4*3,winSize.height-30));
        item6->runAction(moveTo);
        menu1->addChild(item6);
        
        
        
        
        bRet = true;
    }while (0);
    
    return bRet;
}

3、效果图


Cocosd-x3.1 Menu使用示例