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、效果图


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值