zoukankan      html  css  js  c++  java
  • Cocos2d-x文本菜单

    文本菜单是菜单项仅仅是显示文本,文本菜单类包含了MenuItemLabelMenuItemFontMenuItemAtlasFontMenuItemLabel是个抽象类,详细使用的时候是使用MenuItemFontMenuItemAtlasFont两个类。

    文本菜单类MenuItemFont。它的当中一个创建函数create定义例如以下:

    static MenultemAtlasFont*create  ( const std::string &  value, //要显示的文本
     const ccMenuCallback & callback                             //菜单操作的回调函数指针
     )

    文本菜单类MenuItemAtlasFont是基于图片集的文本菜单项,它的当中一个创建函数create定义例如以下:

    static MenuItemAtlasFont* create  ( const std::string &  value, //要显示的文本
     const std::string & charMapFile,                                         //图片集合文件
     int  itemWidth,                                                                       //要截取的文字在图片中的宽度
     int  itemHeight,                                                             //要截取的文字在图片中的高度
     char  startCharMap                                                      //菜单操作的回调函数指针
     )

    这次我们会通过一个实例介绍一下文本菜单的使用,这个实比例如以下图所看到的。当中菜单Start是使用MenuItemFont实现的,菜单Help是使用MenuItemAtlasFont实现的。


    以下我们看看HelloWorldScene.cppinit函数例如以下:

    bool HelloWorld::init()
    {
       if ( !Layer::init() )
       {
           return false;
       }
       
       Size visibleSize = Director::getInstance()->getVisibleSize();
       Point origin = Director::getInstance()->getVisibleOrigin();
       
        Sprite *bg =Sprite::create("menu/background.png");   
       bg->setPosition(Point(origin.x + visibleSize.width/2,
                                 origin.y +visibleSize.height /2));
       this->addChild(bg);
     
       MenuItemFont::setFontName("Times New Roman");                                                       ①
       MenuItemFont::setFontSize(86);                                                                                      ②
       MenuItemFont *item1 = MenuItemFont::create("Start",
                                     CC_CALLBACK_1(HelloWorld::menuItem1Callback,this));                             ③
      
       
       MenuItemAtlasFont *item2 = MenuItemAtlasFont::create("Help",
                                            "menu/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;
    }

    上述代码第①和②行是设置文本菜单的文本字体和字体大小。第③行代码是创建MenuItemFont菜单项对象,它是一个一般文本菜单,create是函数的第一个參数是菜单项的文本内容。第二个參数是点击菜单项回调的函数指针。当中CC_CALLBACK_1宏是定义一个回调函数,并函数与对象绑定在一起,1表示这个函数有一个输出參数。HelloWorld::menuItem1Callback是函数指针,this代表函数所在的对象。

    HelloWorld::menuItem1Callback须要在HelloWorld.h头文件里声明,HelloWorld.h头文件代码例如以下:

    #include "cocos2d.h"
     
    class HelloWorld : public cocos2d::Layer
    {
    public:
       
       virtual bool init();
       static cocos2d::Scene* scene();  
     
        void menuItem1Callback(cocos2d::Ref*pSender);
        void menuItem2Callback(cocos2d::Ref*pSender);
     
       CREATE_FUNC(HelloWorld);
    };

    回调函数代码例如以下。函数中的參数是菜单项MenuItem的实例。

    void HelloWorld::menuItem1Callback(Ref*pSender)
    {
       MenuItem* item = (MenuItem*)pSender;
            log("TouchStart Menu Item %p", item);
    }
     
    void HelloWorld::menuItem2Callback(Ref*pSender)
    {
       MenuItem* item = (MenuItem*)pSender;
            log("TouchHelp Menu Item %p", item);
    }

    HelloWorldScene.cppinit函数中第④行代码是创建一个MenuItemAtlasFont菜单项对象。这样的菜单项是基于图片集的菜单项。

    MenuItemAtlasFont须要将图片集放到资源文件夹Resources下。在本例中我们是将全部的图片都放到一个Resources下的menu文件夹中,所以create函数的第二个參数是"menu/tuffy_bold_italic-charmap.png",要求带有menu路径。

    还有第⑤行代码Menu* mn = Menu::create(item1, NULL)是创建菜单对象,把之前创建的菜单项加入到菜单中。create函数中有是这些菜单项的数组。最后要用NULL结束。

    第⑥行代码mn->alignItemsVertically()是设置菜单项垂直对齐。

    第⑦行代码是this->addChild(mn,1,2)是把菜单对象加入到当前层中。

    很多其它内容请关注最新Cocos图书《Cocos2d-x实战 C++卷》
    本书交流讨论站点:http://www.cocoagame.net
    很多其它精彩视频课程请关注智捷课堂Cocos课程:http://v.51work6.com
    欢迎加入Cocos2d-x技术讨论群:257760386


    《Cocos2d-x实战 C++卷》现已上线。各大商店均已开售:

    京东:http://item.jd.com/11584534.html

    亚马逊:http://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU

    当当:http://product.dangdang.com/23606265.html

    互动出版网:http://product.china-pub.com/3770734

    《Cocos2d-x实战 C++卷》源代码及样章下载地址:

    源代码下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1 

    样章下载地址:http://51work6.com/forum.php?

    mod=viewthread&tid=1157&extra=page%3D1

    欢迎关注智捷iOS课堂微信公共平台


  • 相关阅读:
    面向接口程序设计思想实践
    Block Chain Learning Notes
    ECMAScript 6.0
    Etcd Learning Notes
    Travis CI Build Continuous Integration
    Markdown Learning Notes
    SPRING MICROSERVICES IN ACTION
    Java Interview Questions Summary
    Node.js Learning Notes
    Apache Thrift Learning Notes
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7008114.html
Copyright © 2011-2022 走看看