【发布时间】:2020-11-16 21:30:20
【问题描述】:
在我的游戏中有两个对象(眼睛),我需要让它们朝着点击屏幕的方向转动。我在互联网上找到了一个示例监听器,但它是用于多点触控的,我需要单点触控。
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)
如何实现?
【问题讨论】: