阿J的cocos2d-x学习笔记-元素消消看(三)-菜单的创建

这里我们主要讲一下游戏主菜单的创建和功能,可参考

菜单:http://blog.csdn.net/cen616899547/article/details/9080645

场景切换:http://blog.csdn.net/tonny_guan/article/details/27570063

我们使用最简单的方式创建菜单,没有美术表示很无力。在init()函数中加入了监听返回键和加入背景颜色的代码,不多说,直接上代码:

 

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

    auto layerColorBG = cocos2d::LayerColor::create(cocos2d::Color4B(100, 170, 160,255));

    this->addChild(layerColorBG);

    /

    // 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 theprogress. it's an autorelease object

    /*

    auto closeItem = MenuItemImage::create(

       "CloseNormal.png",

       "CloseSelected.png",

       CC_CALLBACK_1(menuScene::menuCloseCallback, this));

 

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

       origin.y + closeItem->getContentSize().height / 2));

 

    // create menu, it's an autorelease object

    auto menu = Menu::create(closeItem, NULL);

    menu->setPosition(Point(0,0));

    this->addChild(menu, 1);

    */

    auto keylistener = EventListenerKeyboard::create();

    keylistener->onKeyReleased = CC_CALLBACK_2(menuScene::onKeyReleased, this);

     _eventDispatcher->addEventListenerWithSceneGraphPriority(keylistener, this);

    Size sizeOfMenuItem;

    sizeOfMenuItem.width = .5*visibleSize.width;

    sizeOfMenuItem.height = .1*visibleSize.height;

 

 

    auto itemStart = MenuItemFont::create("Start", CC_CALLBACK_1(menuScene::menuStartCallback,this));

    itemStart->setPosition(Point(0,0));

    itemStart->setAnchorPoint(ccp(0,0));

    itemStart->setContentSize(sizeOfMenuItem);

    itemStart->setColor(cocos2d::Color3B::YELLOW);

    itemStart->setTag(1);

 

    auto itemResume = MenuItemFont::create("Resume", CC_CALLBACK_1(menuScene::menuResumeCallback, this));

    itemResume->setPosition(Point(0, 0));

    itemResume->setAnchorPoint(ccp(0, 0));

    itemResume->setContentSize(sizeOfMenuItem);

    itemResume->setColor(cocos2d::Color3B::YELLOW);

    itemResume->setTag(2);

 

    auto itemHelp = MenuItemFont::create("Help", CC_CALLBACK_1(menuScene::menuHelpCallback, this));

    itemHelp->setPosition(Point(0, 0));

    itemHelp->setAnchorPoint(ccp(0, 0));

    itemHelp->setContentSize(sizeOfMenuItem);

    itemHelp->setColor(cocos2d::Color3B::YELLOW);

    itemHelp->setTag(3);

 

    auto itemClose = MenuItemFont::create("Close", CC_CALLBACK_1(menuScene::menuCloseCallback, this));

    itemClose->setPosition(Point(0, 0));

    itemClose->setAnchorPoint(ccp(0, 0));

    itemClose->setContentSize(sizeOfMenuItem);

    itemClose->setColor(cocos2d::Color3B::YELLOW);

    itemClose->setTag(4);

 

    auto menu = Menu::create();

    menu->addChild(itemStart);

    menu->addChild(itemResume);

    menu->addChild(itemHelp);

    menu->addChild(itemClose);

    menu->setPosition(Point(visibleSize.width/4,visibleSize.height*2/3));

    this->addChild(menu);

   

    itemStart->setPosition(ccp(0,0));

    itemResume->setPosition(ccp(0, -sizeOfMenuItem.height));

    itemHelp->setPosition(ccp(0,-2*sizeOfMenuItem.height));

    itemClose->setPosition(ccp(0, -3 * sizeOfMenuItem.height));

 

    auto label=Label::createWithTTF("MENU","fonts/MarkerFelt.ttf", 80);

    label->setPosition(Point(visibleSize.width/2,visibleSize.height-40));

            label->setAnchorPoint(Point::ANCHOR_MIDDLE);

            this->addChild(label);

    return true;

}

 

 

void menuScene::menuCloseCallback(Ref* pSender)

{

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) ||(CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)

    MessageBox("You pressedthe close button. Windows Store Apps do not implement a closebutton.", "Alert");

    return;

#endif

    setFirstTime(true);

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

 

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    exit(0);

#endif

}

void menuScene::menuStartCallback(Ref* pSender)

{

    auto director = Director::getInstance();

    auto scene = challengeModel::createScene();

    director->pushScene(scene);

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    exit(0);

#endif

   

}

void menuScene::menuResumeCallback(Ref* pSender)

{

    auto director = Director::getInstance();

    director->popScene();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    exit(0);

#endif

}

void menuScene::menuHelpCallback(Ref* pSender)

{

    auto director = Director::getInstance();

       auto scene = helpScene::createScene();

       director->pushScene(scene);

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    exit(0);

#endif

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值