在Corona SDK中检测每个多点触控事件的X和Y.

时间:2013-08-23 17:43:08

标签: corona multi-touch touch-event

在Corona SDK中激活多点触控后,有没有办法可以跟踪每次同时触摸的x和y坐标?
一个理想的功能是这样的:
 local function detectMultitouch(touches)
for i = 0, #touches do
print(touches[i].x .. " ".. touches[i].y) end
end

1 个答案:

答案 0 :(得分:2)

你可以试试这个

system.activate("multitouch")

local touches = {}
local touchIDs = {}

local function detectMultitouch()
    for i = 1, #touchIDs do
        print("#"..i.." "..tostring(touchIDs[i]) .." = "..touches[touchIDs[i]].x..","..touches[touchIDs[i]].y)
    end
end

Runtime:addEventListener("touch",function(event)
    if event.phase == "began" then
        touches[event.id] = {}
        touches[event.id].x = event.x
        touches[event.id].y = event.y
        touches[event.id].coords = display.newText(tostring(event.id).." = "..touches[event.id].x..","..touches[event.id].y,0,0,system.nativeFont,15)
        touches[event.id].coords.x = touches[event.id].x
        touches[event.id].coords.y = touches[event.id].y

        table.insert(touchIDs,event.id)
    elseif event.phase == "moved" then
        touches[event.id].x = event.x
        touches[event.id].y = event.y
        touches[event.id].coords.text = tostring(event.id).." = "..touches[event.id].x..","..touches[event.id].y
        touches[event.id].coords.x = touches[event.id].x
        touches[event.id].coords.y = touches[event.id].y - 20
    elseif event.phase == "ended" then
        touches[event.id].coords:removeSelf()
        touches[event.id] = nil
        table.remove(touchIDs,table.indexOf(touchIDs, event.id))


        detectMultitouch(touches)
    end
end)