cocos2dx-lua基础内容之 菜单项的使用

来源:互联网 发布:淘宝培训班多少学费 编辑:程序博客网 时间:2024/05/18 21:11

菜单中又包含了菜单项,菜单项类是MenuItem,每个菜单项都有三个基本状态:正常、选中和禁止。
MenuItemLabel类是文本菜单,它有两个子类:MenuItemAtlasFont和MenuItemFont。
MenuItemSprite类是精灵菜单,它的子类是MenuItemImage属于图片菜单。
MenuItemToggle类是开关菜单。

文本菜单

文本菜单的菜单项只能显示文本,MenuItemLabel是抽象类具体使用时只使用MenuItemFont和MenuItemAtlasFont两个类。
目前的cocos2dx lua中MenuItemAtlasFont还没有移植性,因此还不能使用。可以使用LabelAtlas和MenuItemLabel结合来实现。

精灵菜单和图片菜单

使用MenuItemSprite比较麻烦,需要先创建三种不同状态的精灵。MenuItemSprite还有一些create函数,在这些函数中可以省略无状态的精灵。

如果精灵是由图片构成的,可以使用MenuItemImage实现与精灵菜单同样的效果。

下面贴出代码:

--TestScene.lua文件内容。main.lua文件内容请参考之前同系列的文章。size=cc.Director:getInstance():getVisibleSize()local testScene=class("Test",        function()             return cc.Scene:create()         end        )--初始化function testScene:ctor()endfunction testScene:create()   local scene=testScene.new()           --创建场景。   local layer=testScene:createLayer()   --创建层。   scene:addChild(layer)                 --将层添加到场景中   return scene                          --返回场景endfunction testScene:createLayer()   local layer=cc.Layer:create()                         --创建场景     --MenuItemFont的使用    cc.MenuItemFont:setFontName("Times New Roman")    cc.MenuItemFont:setFontSize(86)    local item1=cc.MenuItemFont:create("Start")    local function menuItemCallback(sender)          --按钮回调函数       cclog("Touch Start Menu Item")    end    item1:registerScriptTapHandler(menuItemCallback) --注册回调函数    --MenuItemAtlasFont的使用:结合LabelAtlas和MenuItemLabel一起使用。    local labelAtals=cc.LabelAtlas:create("Help","tuffy_bold_italic-charmap.png",48,65,string.byte(' '))    local item2=cc.MenuItemLabel:create(labelAtals)    local function menuItem2Callback(sender)       cclog("Touch Help Menu Item")    end    item2:registerScriptTapHandler(menuItem2Callback)    --精灵菜单的使用    local s1=cc.Sprite:create("HelloWorld.png")    local s2=cc.Sprite:create("hero1.png")    local spriteBtn=cc.MenuItemSprite:create(s1,s2)    local function spriteBtnCB(sender)        print("spriteBtnCB")    end    spriteBtn:registerScriptTapHandler(spriteBtnCB)    --图片菜单的使用    local ImageBtn=cc.MenuItemImage:create("hero1.png","HelloWorld.png")    local function imageBtnCB(sender)        print("imageBtnCB")    end    ImageBtn:registerScriptTapHandler(imageBtnCB)    --开关菜单的使用    local toggleBtn=cc.MenuItemToggle:create(spriteBtn,ImageBtn)    local function toggleCB()        print("toggleCB")    end    toggleBtn:registerScriptTapHandler(toggleCB)    --local mn=cc.Menu:create(item1,item2,spriteBtn,ImageBtn)                     --添加到menu中,不需要最后加null    mn:alignItemsVertically() --居中排列    layer:addChild(mn)   return layer                                          --返回层。endreturn testScene

使用菜单项需要注意以下几点:

  • MenuItemFont:create(“Start”) –参数是字符串。

  • MenuItemSprite:create(node1,node2)
    –三个参数,表示三个状态,参数是Sprite类。

  • MenuItemImage:create(“.png”,”.png”)
    –三个参数,意义同上,参数是图片。

  • MenuItemToggle:create(MenuItemSprite:create(),MenuItemSprite:create())–两个参数,参数是上面的菜单项,MenuItemImage、MenuItemSprite、MenuItemFont。

  • 最后上面的菜单项如果需要显示在屏幕上,需要添加到Menu中,如:

local mn=cc.Menu:create(item1,item2,item3) 
阅读全文
0 0
原创粉丝点击