cocos2dx 玩转Menu菜单

前端之家收集整理的这篇文章主要介绍了cocos2dx 玩转Menu菜单前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

MenuScene.cpp

#include "MenuScene.h"
USING_NS_CC;
#include "ui/CocosGUI.h"
using namespace ui;
Scene* MenuScene::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = MenuScene::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 MenuScene::init()
{
    //////////////////////////////
    // 1. super init first
    if (!Layer::init())
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image,which is clicked to quit the program
    // you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object





    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label

    //新增标签类Label
    /*Func 创建Label标签对象 *@parameter1 显示文字内容 *@parameter2 字体名称 *@parameter3 字体大小 */
    auto label0 = Label::createWithSystemFont("Create Label","Marker Felt",32);//这里不能addchild()

    //标签菜单MenuItemLabel
    /*Func 创建标签菜单MenuItemLabel *@parameter1 标签对象 *@parameter2 单击回调 */
        auto labelItem = MenuItemLabel::create(label0,CC_CALLBACK_0(MenuScene::menuPrintCallback,this));
    labelItem->setPosition(visibleSize.width / 2,visibleSize.height*0.9);


    //字体菜单MenuItemFont
    /*Func 创建字体菜单MenuItemFont *@parameter1 显示文字内容 *@parameter2 单击回调 */
    MenuItemFont::setFontName("Courier new");
    MenuItemFont::setFontSize(64);
    auto fontItem = MenuItemFont::create("MenuItemFont",this));
    fontItem->setPosition(visibleSize.width / 2,visibleSize.height*0.6);

    //精灵菜单MenuItemSprite
    /**Func 创建精灵菜单MenuItemSprite *@parameter1 正常状态的精灵对象 *@parameter2 选择状态的精灵对象 *@parameter3 单击回调 */
    auto closeNormal = Sprite::create("CloseNormal.png");
    auto closeSelected = Sprite::create("CloseSelected.png");
    auto closeSpriteItem = MenuItemSprite::create(
        closeNormal,closeSelected,CC_CALLBACK_1(MenuScene::menuCloseCallback,this));

    closeSpriteItem->setPosition(visibleSize.width / 2,visibleSize.height*0.5f);
    //图片精灵菜单MenuItemImage
    auto closeItem = MenuItemImage::create(
        "CloseNormal.png","CloseSelected.png",this));

    closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width / 2,origin.y + closeItem->getContentSize().height / 2));

    //触发器菜单MenuItemToggle
    /*Func 创建触发器菜单MenuItemToggle *@parameter1 单击回调 *@parameter2 MenuItem对象 *@parameter3 MenuItem对象 */
    auto toggleItem = MenuItemToggle::createWithCallback(
        CC_CALLBACK_1(MenuScene::menuItemToggle,this),MenuItemFont::create("On"),MenuItemFont::create("Off"),NULL);
    toggleItem->setPosition(visibleSize.width / 2,visibleSize.height*0.1f);
    // create menu,it's an autorelease object
    auto memu_ = Menu::create(labelItem,fontItem,closeSpriteItem,toggleItem,closeItem,NULL);
    memu_->setPosition(Vec2::ZERO);
    this->addChild(memu_,2);
    /* *CC_CALLBACK_0 *CC_CALLBACK_1 表示调用的时候传递一个参数 */
    return true;
}

void MenuScene::menuPrintCallback()
{
    log("callback menuPrintCallback-->");
}
void MenuScene::menuItemToggle(Ref * pSender)
{
    log("selected index: %d",dynamic_cast<MenuItemToggle*>(pSender)->getSelectedIndex());
}
void MenuScene::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
    MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGE`这里写代码片`T_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

MenuScene.h

#ifndef __MENUSCENE_SCENE_H__
#define __MENUSCENE_SCENE_H__

#include "cocos2d.h"
class MenuScene : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp,so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();
    // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'id' in cocos2d-iphone
    virtual bool init();

    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);

    // implement the "static create()" method manually
    CREATE_FUNC(MenuScene);
    void menuPrintCallback();
    void menuItemToggle(Ref * pSender);
};

#endif // __MenuScene_SCENE_H__
原文链接:https://www.f2er.com/cocos2dx/341599.html

猜你在找的Cocos2d-x相关文章