Cocos2d-X游戏开发(六)

Cocos2d-X游戏开发

  1. 菜单

菜单Menu是专门用来承载菜单按钮的Layer图层,图层中的子节点只能够是菜单项MenuItem或其子类。通常先创建菜单项MenuItem,然后使用一个或多个菜单项生成菜单Menu,最后把Menu加入当前Layer图层。

如果直接在层中添加MenuItem也可以正常显示,但是无法响应回调函数,因为Menu是继承至Layer,也就继承了触摸的相关事件,而MenuItem只是从Node继承而来,并不响应触摸,因此无法调用回调函数。

  1. 菜单项的子类可以分成三类,总共六个:
  2. 文字菜单项:MenuItemLabelMenuItemFont
  3. 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);

结果:

 

  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);

结果:

 

 

  1. 图片菜单项:MenuItemSpriteMenuItemImage
  2. 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);

结果:

  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);

  1. 触发器菜单项:MenuItemToggle
  2. 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);

  1. 回调函数:

/*菜单回调方法*/

voidStartLayer::menuNewGameCallback(Ref* pSender)

{

    intvalue = dynamic_cast<MenuItemToggle*>(pSender)->getSelectedIndex();

    log("%d", value);

}

  1. 结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值