cocos2d-x 3.0菜单的使用

来源:互联网 发布:移动宽带端口扩容申请 编辑:程序博客网 时间:2024/06/06 08:36
菜单中又包含了菜单项,菜单项类是MenuItem,每个菜单项都有三个基本状态:正常、选中、禁止。
菜单分类是按照菜单项进行分类的。
MenuItem的子类有
MenuItemLabel(文本)
MenuItemSprite(精灵)

MenuItemToggle(开关)

其中MenuItemLabel类有两个子类MenuItemAtlasFont和MenuItemFont;
MenuItemSprite类是精灵菜单,它的子类是MenuItemImage,是图片菜单;
MenuItemToggle是开关菜单。

1、文本菜单类MenuItemLabel,它的创建函数create定义如下:
static MenuItemLabel * create(Node*label, const ccMenuCallback& callback);
static MenuItemLabel* create(Node *label);

2、MenuItemFont是继承MenuItemLabel的,所以它有MenuItemFont的一些特性,它的创建函数create如下定义:
static MenuItemFont * create(const std::string& value = "");
static MenuItemFont * create(const std::string& value, const ccMenuCallback& callback);

3、MenuItemAtlasFont是继承MenuItemLabel的,所以它有MenuItemFont的一些特性,它是基于图片集的文本菜单项,它的创建函数create如下定义:
static MenuItemAtlasFont* create(
const std::string& value, 
const std::string& charMapFile, 
int itemWidth, int itemHeight, 
char startCharMap);

static MenuItemAtlasFont* create(
const std::string& value, 
const std::string& charMapFile, 
int itemWidth, int itemHeight, 
char startCharMap, 
const ccMenuCallback& callback);
第一个参数是要显示的文本,第二个是图片集文件,第三、四个是要截取的文字在图片的宽和高,第五个是图片集文件的开始字符

4、精灵菜单项类的其中一个创建函数create定义如下:
static MenuItemSprite * create(
Node* normalSprite, 
Node* selectedSprite, 
const ccMenuCallback& callback);
参数一是菜单项正常显示的精灵,参数二是选择菜单项显示的精灵。

使用MenuItemSprite 比较麻烦,在创建MenuItemSprite 之前要先创建三种不同状态时的精灵(normalSprite、selectorSp rite、disabledSprite)。MenuItemSprite 还有一些create函数,在这些create函数中可以省略disabledSprite参数。

5、如果精灵是由图片构成的可以使用MenuIte mImage实现与精灵菜单同样的效果。它的其中一个创建函数create定义如下:
static MenuItemImage* create(
const std::string&normalImage, 
const std::string&selectedImage, 
const ccMenuCallback& callback);

6、开关菜单的菜单项类是MenuItemToggle,它是一种可以进行两种状态切换的菜单项。
它可以通过下面的函数创建:
static MenuItemToggle * createWithCallback(
const ccMenuCallback& callback, 
const Vector<MenuItem*>& menuItems);
参数二是进行切换的菜单项

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

0 0