第五章菜单

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

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 +
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值