声明按钮TAG标签
local TAGEBTN_TEST1 =0x01
local TAGEBTN_TEST2 =0x02
绑定事件 binBtn(“Button_1”,TAGEBTN_TEST1) binBtn(“Button_2”,TAGEBTN_TEST2 )
function binBtn(nameString,tagNum)
local function RuleBtnCallBack(sender, eventType)
local ntag = sender:getTag() –获取标签
if eventType == ccui.TouchEventType.began then
sender:setColor(cc.c3b(180, 180, 180)) –点击开始时改变颜色
elseif eventType ~= ccui.TouchEventType.moved then
sender:setColor(cc.c3b(255, 255, 255)) –结束恢复
end
if eventType == ccui.TouchEventType.ended then
if ntag ==TAGEBTN_TEST1 then 检测 点击的按钮
print(”点击按钮1执行方法”)
end
if ntag ==TAGEBTN_TEST2 then
print(”点击按钮2执行方法”)
end
return
end
–注册事件
local btn = createRoomMian:getChildByName(nameString)
btn:setTag(tagNum)
btn:addTouchEventListener(RuleBtnCallBack)
return btn