第五章菜单

本文档详细介绍了Cocos2d-x中的菜单相关类,包括Menu和MenuItem,特别是文本菜单(MenuItemLabel、MenuItemFont)、精灵菜单(MenuItemSprite、MenuItemImage)以及开关菜单(MenuItemToggle)。通过实例展示了如何创建和使用这些菜单类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cocos2d-x学习笔记


菜单相关类

菜单相关类包含:菜单类Menu和菜单选项类MenuItem,Menu类派生于Layer。

文本菜单

文本菜单是菜单项,只能显示文本,文本菜单类包括MenuItemLabelMenuItemFontMenuItemAtlasFont。MenuItemLabel是个抽象类,具体使用的时候是MenuItemFont和MenuItemAtlasFont两个类。
文本菜单类MenuItemFont中的一个创建函数create定义如下:

static MenuItemFont * create ( const std::string & value ,//要显示的文本
        const ccMenuCallback & callback)//菜单操作的回调函数指针
文本菜单类MenuItemAtlasFont是基于图片集的文本菜单项,其中一个创建函数create定义如下:
static MenuItemAtlasFont * create ( cosnt std::string & value,//要显示的文本
        const std::string & charMapFile,//图片集文件
        int itemWidth,//要截取的文字在图片中的宽度
        int itemHeight,//要截取的文字在图片中的高度
        char startCharMap,//开始字符
        consr ccMenuCallback & callback)//菜单操作的回调函数指针

实例

HelloWorld.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();

    virtual bool init();

    // a selector callback
    void menuItem1Callback(cocos2d::Ref * pSender);//菜单回调函数
    void menuItem2Callback(cocos2d::Ref * pSender);//菜单回调函数

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorld.cpp文件

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /
    // 2. add you code
    Sprite * bg = Sprite::create("menu/background.png");
    bg->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));

    this->addChild(bg);

    MenuItemFont::setFontName("Times New Roman");//设置字体
    MenuItemFont::setFontSize(86);//设置大小
    MenuItemFont * item1 = MenuItemFont::create("Start", CC_CALLBACK_1(HelloWorld::menuItem1Callback, this));//设置内容和回调函数指针

    MenuItemAtlasFont * item2 = MenuItemAtlasFont::create("Help", "menu/tuffy_bold_italic-charmap.png", 48, 65, ' ', CC_CALLBACK_1(HelloWorld::menuItem2Callback, this));

    Menu * mn = Menu::create(item1, item2, NULL);//创建菜单对象,并把之前创建的菜单项加入菜单对象中,最后以NULL结尾
    mn->alignItemsVertically();//设置菜单项垂直对齐
    this->addChild(mn);

    return true;
}

void HelloWorld::menuItem1Callback(cocos2d::Ref * pSender)
{
    MenuItem * item = (MenuItem *)pSender;
    log("Touch Start Menu Item % p", item);
}

void HelloWorld::menuItem2Callback(cocos2d::Ref * pSender)
{
    MenuItem * item = (MenuItem *)pSender;
    log("Touch Help Menu Item % p", item);
}

精灵菜单和图片菜单

精灵菜单的菜单项类是MenuItemSprite,图片菜单的菜单项类是MenuItemImage。由于MenuItemImage继承于MenuItemSprite,所以图片菜单也属于精灵菜单。
精灵菜单类MenuItemSprite,它的其中一个创建函数create定义如下:

static MenuItemSprite * create ( Node * normalSprite,//菜单项正常显示时的精灵
     Node * selectedSprite;//选择菜单项时的精灵
     Node * disalbleSprite;//菜单项禁用时的精灵
     const ccMenuCallback * callback )//菜单操作的回调函数指针

如果精灵是由图片构成的,我们可以使用MenuItemImage实现与精灵菜单同样的效果。其中一个定义create如下:

static MenuItemImage * create ( const std::string & normalImage,//菜单项正常显示的的图片
    const std::string & selectedImage,//选择菜单项时的图片
    const std::string & disabledImage, //菜单项禁用时的图片
    const ccMenuCallback & callback)//菜单操作的回调函数指针

实例

HelloWorld.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();

    virtual bool init();

    // a selector callback
    void menuItemStartCallback(Ref * pSender);
    void menuItemSettingCallback(Ref * pSender);
    void menuItemHelpCallback(Ref * pSender);

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorld.cpp文件

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /
    // 2. add you code
    Sprite * bg = Sprite::create("menu/background.png");
    bg->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));

    this->addChild(bg);

    //开始精灵
    Sprite * startSpriteNormal = Sprite::create("menu/start-up.png");
    Sprite * startSpriteSelected = Sprite::create("menu/start-down.png");

    MenuItemSprite * startMenuItem = MenuItemSprite::create(startSpriteNormal, startSpriteNormal, CC_CALLBACK_1(HelloWorld::menuItemStartCallback, this));
    startMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(700, 170)));

    //设置图片菜单项
    MenuItemImage * settingMenuItem = MenuItemImage::create("menu/setting-up.png", "menu/setting-down.png", CC_CALLBACK_1(HelloWorld::menuItemSettingCallback, this));
    settingMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(480, 400)));

    //帮助图片菜单选项
    MenuItemImage * helpMenuItem = MenuItemImage::create("menu/help-up.png", "menu/help-down.png", CC_CALLBACK_1(HelloWorld::menuItemHelpCallback, this));
    helpMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(860, 480)));

    Menu * mu = Menu::create(startMenuItem, settingMenuItem, helpMenuItem, NULL);
    mu->setPosition(Vec2::ZERO);//等价于Vec2(0.0f, 0.0f)
    this->addChild(mu);

    return true;
}

void HelloWorld::menuItemStartCallback(Ref * pSender)
{

}

void HelloWorld::menuItemSettingCallback(Ref * pSender)
{

}

void HelloWorld::menuItemHelpCallback(Ref * pSender)
{

}

开关菜单

开关菜单的菜单选项类是MenuITemToggle,它是一种可以进行两种状态切换的菜单项。

static MenuItemToggle * createWithCallback(
    const ccMenuCallback & callback,//菜单操作的回调函数指针
    MenuItem * item,//进行切换的菜单项
    ···
)

从第二个参数开始都是MenuItem类的实例对象,它们是开关菜单显示的菜单项,它们可以使文本、图片和精灵类型的菜单项,但是最后要以NULL结尾。

实例

HelloWorld.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();

    virtual bool init();

    // a selector callback
    void menuSoundToggleCallback(Ref * pSender);
    void menuMusicToggleCallback(Ref * pSender);

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorld.cpp文件

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /
    // 2. add you code
    Sprite * bg = Sprite::create("menu/setting-back.png");
    bg->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));

    this->addChild(bg);

    //音效
    auto soundOnMenuItem = MenuItemImage::create("menu/on.png", "menu/off.png");
    auto soundOffMenuItem = MenuItemImage::create("menu/off.png", "menu/off.png");  

    auto soundToggleMenuItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(HelloWorld::menuSoundToggleCallback, this), soundOnMenuItem, soundOffMenuItem, NULL);
    soundToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(818, 220)));

    //音乐
    auto musicOnMenuItem = MenuItemImage::create("menu//on.png", "menu/off.png");
    auto musicOffMenuItem = MenuItemImage::create("menu/off.png", "menu/off.png");
    auto musicToggleMenuItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(HelloWorld::menuMusicToggleCallback, this), musicOnMenuItem, musicOffMenuItem, NULL);
    musicToggleMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(818, 362)));

    //OK按钮
    auto okMenuItem = MenuItemImage::create("menu/ok-down.png", "menu/ok_up.png");
    okMenuItem->setPosition(Director::getInstance()->convertToGL(Vec2(600, 510)));

    Menu * mu = Menu::create(soundToggleMenuItem, musicToggleMenuItem, okMenuItem,NULL);
    mu->setPosition(Vec2::ZERO);
    this->addChild(mu);

    return true;
}

void HelloWorld::menuSoundToggleCallback(Ref * pSender)
{

}

void HelloWorld::menuMusicToggleCallback(Ref * pSender)
{

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值