Cocos2d-x Lua Layer:onTouch

在cocos2dx Lua工程的源码中 LayerEx.lua中 有扩充的onTouch方法如下:

function Layer:onTouch(callback, isMultiTouches, swallowTouches)
if type(isMultiTouches) ~= “boolean” then isMultiTouches = false end
if type(swallowTouches) ~= “boolean” then swallowTouches = false end

self:registerScriptTouchHandler(function(state, ...)
    local args = {...}
    local event = {name = state} -- 点击类型  began,moved, ended
    if isMultiTouches then --多点触摸
        args = args[1]
        local points = {}
        for i = 1, #args, 3 do
            local x, y, id = args[i], args[i + 1], args[i + 2]
            points[id] = {x = x, y = y, id = id} 
        end
        event.points = points
    else -- 单点触摸
        event.x = args[1]
        event.y = args[2]
    end
    return callback( event )
end, isMultiTouches, 0, swallowTouches)
self:setTouchEnabled(true)
return self

end

使用:
self:onTouch(function (event)
print(“—-“, event.x, event.y, event.name)
return true – 切记返回true
end, nil, true)

你可能感兴趣的:(Cocos2d_x,Lua)