cocosdx3.0 lua项目 从cocostudio中到处得ui 获取button 以及button监听

    local winsize=cc.Director:getInstance():getWinSize()
    local layer=cc.Layer:create()
    local scene=cc.Scene:create()
    n=2
    local function touchEvent(sender,eventType)

        cclog(sender:getTag())
        if eventType == ccui.TouchEventType.began then
            print("touchbegin")
        elseif eventType == ccui.TouchEventType.moved then
            print("Touch Move")
        elseif eventType == ccui.TouchEventType.ended then
        --只能出现创建一棵树
            -- local sprite = layer:getChildByTag(sender:getTag())
            local sprite
            if sprite ==nil then
                sprite = cc.Sprite:create(sender:getTag()..".png")
                sprite:setPosition(300,300)
                layer:addChild(sprite,3,n)
                n=n+1
            end
            print("Touch Up")
        elseif eventType == ccui.TouchEventType.canceled then
            print("Touch Cancelled")
        end
    end  

    local menu = ccs.GUIReader:getInstance():widgetFromJsonFile("Menu_1.json")
    layer:addChild(menu,1)
    i = 2
    while i <= 6 do
    print("i is now " .. i)
        local button2=menu:getChildByTag(i)
        if button2==nil then 
            cclog("button2==nil")
        else
            button2:addTouchEventListener(touchEvent)  
        end
        i=i+1
    end  
注意在lua中需要再加上
require "Cocos2d"
require "GuiConstants" 才可以实现监听button

你可能感兴趣的:(lua,cocos2dx)