Cocos2d-X游戏开发
-
菜单
菜单Menu是专门用来承载菜单按钮的Layer图层,图层中的子节点只能够是菜单项MenuItem或其子类。通常先创建菜单项MenuItem,然后使用一个或多个菜单项生成菜单Menu,最后把Menu加入当前Layer图层。
如果直接在层中添加MenuItem也可以正常显示,但是无法响应回调函数,因为Menu是继承至Layer,也就继承了触摸的相关事件,而MenuItem只是从Node继承而来,并不响应触摸,因此无法调用回调函数。
-
菜单项的子类可以分成三类,总共六个:
-
文字菜单项:MenuItemLabel、MenuItemFont;
-
MenuItemLabel标签菜单项:
//读取菜单数据
Dictionary *strings = Dictionary::createWithContentsOfFile("menu.xml");
constchar *startmenu = ((String*)strings->objectForKey("startmenu"))->getCString();
//添加一个游戏菜单
autolabStartMenu = Label::createWithTTF(startmenu, "fonts/simhei.ttf", 24 * 2);
//添加标签菜单,添加回设函数
autostartItem = MenuItemLabel::create(labStartMenu, this, menu_selector(StartLayer::menuNewGameCallback));
//设定标记与位置
startItem->setTag(1);
startItem->setPosition(Vec2(origin.x + visibleSize.width / 2,
origin.y + visibleSize.height / 2));
//创建菜单对象
Menu* pMenu = Menu::create(startItem, NULL);
//设定位置
pMenu->setPosition(Vec2::ZERO);
//添加菜单到图层
this->addChild(pMenu, 1);
结果:
-
MenuItemFont字体菜单项:
//读取菜单数据
Dictionary *strings = Dictionary::createWithContentsOfFile("menu.xml");
constchar *startmenu = ((String*)strings->objectForKey("startmenu"))->getCString();
//设置字体大小
MenuItemFont::setFontSize(64);
//设置字体
MenuItemFont::setFontName("simhei");
//添加一个游戏菜单项,设定菜单字体大小
autostartMenuItem = MenuItemFont::create(startmenu, [&](Ref* sender)
{
//添加回调
log("菜单项回调!"); }
);
//设置菜单项大小
startMenuItem->setPosition(Point(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));
//创建菜单对象
Menu* pMenu = Menu::create(startMenuItem, NULL);
//设定位置
pMenu->setPosition(Vec2::ZERO);
//添加菜单到图层
this->addChild(pMenu, 1);
结果:
-
图片菜单项:MenuItemSprite、MenuItemImage;
-
MenuItemSprite精灵菜单项
autostartNormal = Sprite::create("start.png");
autostartSelected = Sprite::create("startSelect.png");
autostartSpriteItem = MenuItemSprite::create(startNormal, startSelected, CC_CALLBACK_1(StartLayer::menuNewGameCallback, this));
startSpriteItem->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
//创建菜单对象
Menu* pMenu = Menu::create(startSpriteItem, NULL);
//设定位置
pMenu->setPosition(Vec2::ZERO);
//添加菜单到图层
this->addChild(pMenu, 1);
结果:
-
MenuItemImage图像菜单项
autostartNormal = MenuItemImage::create("start.png", "startSelect.png", CC_CALLBACK_1(StartLayer::menuNewGameCallback, this));
startNormal->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
//创建菜单对象
Menu* pMenu = Menu::create(startNormal, NULL);
//设定位置
pMenu->setPosition(Vec2::ZERO);
//添加菜单到图层
this->addChild(pMenu, 1);
-
触发器菜单项:MenuItemToggle。
-
MenuItemToggle触发器菜单项
//读取菜单数据
Dictionary *strings = Dictionary::createWithContentsOfFile("menu.xml");
constchar *startmenu = ((String*)strings->objectForKey("startmenu"))->getCString();
constchar *gamequit = ((String*)strings->objectForKey("gamequit"))->getCString();
//设置字体大小
MenuItemFont::setFontSize(64);
//设置字体
MenuItemFont::setFontName("simhei");
//创建触发器按钮,添加内容与回调
autotoggleItem = MenuItemToggle::createWithCallback(CC_CALLBACK_1(StartLayer::menuNewGameCallback, this), MenuItemFont::create(startmenu), MenuItemFont::create(gamequit), NULL);
toggleItem->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
//创建菜单对象
Menu* pMenu = Menu::create(toggleItem, NULL);
//设定位置
pMenu->setPosition(Vec2::ZERO);
//添加菜单到图层
this->addChild(pMenu, 1);
-
回调函数:
/*菜单回调方法*/
voidStartLayer::menuNewGameCallback(Ref* pSender)
{
intvalue = dynamic_cast<MenuItemToggle*>(pSender)->getSelectedIndex();
log("%d", value);
}
-
结果: