Cocos Lua的Touch 點擊事件添加

兩種方式:spa

-- 觸摸開始
    local function onTouchBegan(touch, event)
        return true
    end


    -- 觸摸結束
    local function onTouchEnded(touch, event)
        -- 獲取點擊位置
        local location = touch:getLocation()
        
        print(location.x .. "  "..location.y)
    end


    local listener = cc.EventListenerTouchOneByOne:create()
    -- 註冊兩個回調監聽方法
    listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
    listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
    local eventDispatcher = self:getEventDispatcher()-- 事件派發器
    -- 綁定觸摸事件到層當中
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self)

這種方式比較複雜點,針對Node均可以

 

第二種方式:code

self.bet_panel:addTouchEventListener(function(sender, state)
        if state == 0 then
            print("start")           
        elseif state == 1 then
        elseif state == 2 then
            print("ended")            
        else 
            print("canceled")            
        end
    end)     這種方式主要是真的UINode
相關文章
相關標籤/搜索