首页 > 代码库 > Cocos2d-x学习笔记(七)菜单

Cocos2d-x学习笔记(七)菜单

文本菜单示例:

#include "HelloWorldScene.h"USING_NS_CC;Scene* HelloWorld::createScene(){    auto scene = Scene::create();    auto layer = HelloWorld::create();    scene->addChild(layer);    return scene;}bool HelloWorld::init(){    if ( !Layer::init() )    {        return false;    }        Size visibleSize = Director::getInstance()->getVisibleSize();    Vec2 origin = Director::getInstance()->getVisibleOrigin();    Sprite *bg = Sprite::create("bg.jpg");    bg->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2));    this->addChild(bg);    MenuItemFont::setFontName("Times New Roman");    MenuItemFont::setFontSize(80);    MenuItemFont *item1 = MenuItemFont::create("Start", CC_CALLBACK_1(HelloWorld::menuItem1Callback, this));
  MenuItemAtlasFont *item2 = MenuItemAtlasFont::create("Help", "fonts/tuffy_bold_italic-charmap.png",48, 65 ,‘ ‘, CC_CALLBACK_1(HelloWorld::menuItem2Callback, this));

Menu *mn = Menu::create(item1, item2, 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);}

运行效果:

技术分享

图1 文本菜单示例

 

Cocos2d-x学习笔记(七)菜单