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

    文本菜单是菜单项只是显示文本,文本菜单类包括了MenuItemLabelMenuItemFontMenuItemAtlasFontMenuItemLabel是个抽象类,具体使用的时候是使用MenuItemFontMenuItemAtlasFont两个类。

    文本菜单类MenuItemFont,它的其中一个创建函数create定义如下:

     

     

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. static MenultemAtlasFont*create  ( const std::string &  value, //要显示的文本  
    2.  const ccMenuCallback & callback                             //菜单操作的回调函数指针  
    3.  )  

     

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

     

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. static MenuItemAtlasFont* create  ( const std::string &  value, //要显示的文本  
    2.  const std::string & charMapFile,                                         //图片集合文件  
    3.  int  itemWidth,                                                                       //要截取的文字在图片中的宽度  
    4.  int  itemHeight,                                                             //要截取的文字在图片中的高度  
    5.  char  startCharMap                                                      //菜单操作的回调函数指针  
    6.  )  

     

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

     

    下面我们看看HelloWorldScene.cppinit函数如下:

     

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. bool HelloWorld::init()  
    2. {  
    3.    if ( !Layer::init() )  
    4.    {  
    5.        return false;  
    6.    }  
    7.      
    8.    Size visibleSize = Director::getInstance()->getVisibleSize();  
    9.    Point origin = Director::getInstance()->getVisibleOrigin();  
    10.      
    11.     Sprite *bg =Sprite::create("menu/background.png");     
    12.    bg->setPosition(Point(origin.x + visibleSize.width/2,  
    13.                              origin.y +visibleSize.height /2));  
    14.    this->addChild(bg);  
    15.    
    16.    MenuItemFont::setFontName("Times New Roman");                                                       ①  
    17.    MenuItemFont::setFontSize(86);                                                                                      ②  
    18.    MenuItemFont *item1 = MenuItemFont::create("Start",  
    19.                                  CC_CALLBACK_1(HelloWorld::menuItem1Callback,this));                             ③  
    20.     
    21.      
    22.    MenuItemAtlasFont *item2 = MenuItemAtlasFont::create("Help",  
    23.                                         "menu/tuffy_bold_italic-charmap.png",48, 65, ' ',  
    24.                                    CC_CALLBACK_1(HelloWorld::menuItem2Callback,this));                         ④  
    25.      
    26.    Menu* mn = Menu::create(item1, item2, NULL);                                                                         ⑤  
    27.    mn->alignItemsVertically();                                                                                               ⑥  
    28.    this->addChild(mn);                                                                                                         ⑦  
    29.      
    30.    return true;  
    31. }  

     

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

    HelloWorld::menuItem1Callback需要在HelloWorld.h头文件中声明,HelloWorld.h头文件代码如下:

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #include "cocos2d.h"  
    2.    
    3. class HelloWorld : public cocos2d::Layer  
    4. {  
    5. public:  
    6.      
    7.    virtual bool init();  
    8.    static cocos2d::Scene* scene();    
    9.    
    10.     void menuItem1Callback(cocos2d::Ref*pSender);  
    11.     void menuItem2Callback(cocos2d::Ref*pSender);  
    12.    
    13.    CREATE_FUNC(HelloWorld);  
    14. };  

     

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

     

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. void HelloWorld::menuItem1Callback(Ref*pSender)  
    2. {  
    3.    MenuItem* item = (MenuItem*)pSender;  
    4.         log("TouchStart Menu Item %p", item);  
    5. }  
    6.    
    7. void HelloWorld::menuItem2Callback(Ref*pSender)  
    8. {  
    9.    MenuItem* item = (MenuItem*)pSender;  
    10.         log("TouchHelp Menu Item %p", item);  
    11. }  

     

    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)是把菜单对象添加到当前层中。

  • 相关阅读:
    第0次作业
    第4次作业
    第3次作业
    第2次作业
    C#浮点数保留位数
    第0次作业
    软件工程第4次作业
    软件工程第3次作业
    软件工程第2次作业
    软件工程第1次作业
  • 原文地址:https://www.cnblogs.com/iOS-Blog/p/3721869.html
Copyright © 2011-2022 走看看