cocos-lua 获取鼠标点击位置

由于测试需要,找了好久才找到相关的代码,emmm

接下来贴一下这部分代码吧

local event_layout = ccui.Layout:create()

function GameTest:get_mouse_pos()

    local function onTouchBegan(touch, event)  
        return true  
    end  

    local function onTouchEnded(touch, event)  
        local location = touch:getLocation()
        local event_x = location["x"] or 0
        local event_y = location["y"] or 0
    end  
 
    local listener = cc.EventListenerTouchOneByOne:create()  
    listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )  
    listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )

    local eventDispatcher = event_layout:getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, event_layout) 

end

然后就能拿到鼠标点击的位置了

你可能感兴趣的:(cocos-lua,coocs2d-x)