cocos2dx-3.3 lua实现一个单选按钮

前端之家收集整理的这篇文章主要介绍了cocos2dx-3.3 lua实现一个单选按钮前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

cocos2dx_3.3 lua实现一个单选按钮

实现单选按钮主要是为了切换的时候更加明显的提醒用户,这里主要的做法是用了cocos2d-x lua里面的cc.Menu的api,老实说,这个引擎封装的api更加的丰富,用起来也很巧妙,主要是看怎么用,我这里只是用简单的方法实现出来,我是用Cocos Code IDE去编辑的,只有一个main.lua的文件,请看下面的代码

tab = {} -- 这里是个table,存储一个cc.MenuItemImage
local item1_1 = 1  --这些是标记
local item1_2 = 2
local item1_3 = 3
local item1_4 = 4
local item1_5 = 5


local TTF = "fonts/Marker Felt.ttf"
require "Cocos2d"
require "Cocos2dConstants"
require "AudioEngine"

-- cclog
cclog = function(...)
    print(string.format(...))
end

-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
    cclog("----------------------------------------")
    cclog("LUA ERROR: " .. tostring(msg) .. "\n")
    cclog(debug.traceback())
    cclog("----------------------------------------")
    return msg
end

local function main()
    collectgarbage("collect")
    -- avoid memory leak
    collectgarbage("setpause",100)
    collectgarbage("setstepmul",5000)

    -- initialize director
    local director = cc.Director:getInstance()
    local glview = director:getOpenGLView()
    if nil == glview then
        glview = cc.GLView:createWithRect("HelloLua",cc.rect(0,960,640))
        director:setOpenGLView(glview)
    end

    --turn on display FPS
    director:setDisplayStats(true)

    --set FPS. the default value is 1.0/60 if you don't call this
    director:setAnimationInterval(1.0 / 60)

    cc.FileUtils:getInstance():addSearchPath("src")
    cc.FileUtils:getInstance():addSearchPath("res")

    local visibleSize = cc.Director:getInstance():getVisibleSize()
    local origin = cc.Director:getInstance():getVisibleOrigin()

    local function createLayerFarm()
        local layerFarm = cc.Layer:create()

        -- add in farm background
        local bg = cc.Sprite:create("farm.jpg")
        bg:setPosition(origin.x + visibleSize.width / 2 + 80,origin.y + visibleSize.height / 2)
        layerFarm:addChild(bg)
        
        --设置显示数字的文本  
        local txt = cc.Label:create()  
        txt:setString( "wuxinchagn" )  
        txt:setSystemFontSize( 50 )  
        txt:setSystemFontName( "Arial" ) 
        txt:setPosition(origin.x + visibleSize.width / 2,origin.y + visibleSize.height - txt:getContentSize().height)
        txt:setAnchorPoint(  cc.p( 0.5,0.5 ) )  
        local fade = cc.FadeOut:create(1.0)
        local fade_in = fade:reverse()
        

        local seq = cc.Sequence:create(fade,fade_in)
        local repeatAction = cc.RepeatForever:create(seq)
        txt:runAction(repeatAction)
        layerFarm:addChild(txt,2,1)
        
        
        
        local function menuCallback(tag,sender)
            --第一个参数是number型,第二个才是菜单调用传进来的node
            for k,v in pairs(tab) do
                if v:getTag() == sender:getTag() then
                    v:setEnabled(false)  --先设置不可以点击
                    v:selected()         --然后才设置选中的状态
                    print("menuCallback == " .. sender:getTag())
                    local str = "menuCallback == " .. sender:getTag()
                    txt:setString( str )
                    txt:setColor(cc.c3b(255,0))
                    
                else
                    v:setEnabled(true) 
                    v:unselected()
                    
                end
            end
        end
        
        local item1 = cc.MenuItemImage:create("btn/1.png","btn/1-1.png")  --创建一个图片菜单
        item1:registerScriptTapHandler(menuCallback)  --监听这个函数
        item1:setTag(item1_1) --设置这个item1的tag
        table.insert(tab,item1) --插入到一个表中
        
        local item2 = cc.MenuItemImage:create("btn/2.png","btn/2-2.png")
        item2:registerScriptTapHandler(menuCallback)
        item2:setTag(item1_2)
        table.insert(tab,item2)
        
        local item3 = cc.MenuItemImage:create("btn/3.png","btn/3-3.png")
        item3:registerScriptTapHandler(menuCallback)
        item3:setTag(item1_3)
        table.insert(tab,item3)
        
        local item4 = cc.MenuItemImage:create("btn/4.png","btn/4-4.png")
        item4:registerScriptTapHandler(menuCallback)
        item4:setTag(item1_4)
        table.insert(tab,item4)
        
        local item5 = cc.MenuItemImage:create("btn/5.png","btn/5-5.png")
        item5:registerScriptTapHandler(menuCallback)
        item5:setTag(item1_5)
        table.insert(tab,item5)
        
        
        local menu = cc.Menu:create()
        menu:addChild(item1)
        menu:addChild(item2)
        menu:addChild(item3)
        menu:addChild(item4)
        menu:addChild(item5)
        menu:setAnchorPoint(0,0)
        menu:setPosition(visibleSize.width / 3,visibleSize.height / 2)
        menu:alignItemsVertically()  --设置垂直排列
        menu:setScale(1.5) --设置menu的大小
        layerFarm:addChild(menu)  --添加到层中
        return layerFarm --返回到一个层
    end
    
    cclog("run here")
    local sceneGame = cc.Scene:create() --创建场景
    sceneGame:addChild(createLayerFarm())  --将层添加到场景中
    
    cclog("run here2")

    if cc.Director:getInstance():getRunningScene() then 
        cc.Director:getInstance():replaceScene(sceneGame)
    else
        cc.Director:getInstance():runWithScene(sceneGame)
    end
end 

local status,msg = xpcall(main,__G__TRACKBACK__)
if not status then
    error(msg)
end
运行了上面的代码之后就是下面这个



在用lua写的时候,记住一定要把顺序理清了,不然的话,是不会达到你想要的效果的,或者说有bug,lua它是有顺序需求的,多写一下代码就能发现里面的规律了,谢谢
原文链接:https://www.f2er.com/cocos2dx/345792.html

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