1. 程式人生 > >cocos2DX-lua 點選事件封裝

cocos2DX-lua 點選事件封裝

宣告按鈕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