在我的遊戲中,我使用觸摸事件來控制對象。當我觸摸屏幕的右半部分時,對象旋轉,當我觸摸屏幕的左半部分時,對象移動。當它是單點觸摸時,它可以很好地工作,但是當我觸摸屏幕的任何一側,然後同時開始觸摸另一側時,就會產生意想不到的混合行爲。檢測多個觸摸
我想我的問題是,如何分離或區分多個觸摸之一。
system.activate("multitouch")
onTouch = function (event)
if (event.phase == "began") then
pX = event.x -- Get start X position of the touch
print("ID:"..tostring(event.id))
if (event.x > centerX) then --if the touch is in the right or left half of the screen
xPos = "right"
else
xPos = "left"
end
elseif (event.phase == "moved") then
local dX = (event.x - pX)
if (xPos == "right") then
rotatePlayer(dx)
else
movePlayer(dX)
end
更新:
system.activate("multitouch")
local touchID = {} --Table to hold touches
onTouch = function (event)
if (event.phase == "began") then
print("ID:"..tostring(event.id))
if (event.x > centerX) then --if the touch is in the right or left half of the screen
touchID[event.id] = {}
touchID[event.id].x = event.x
xPos = "right"
pX = touchID[event.id].x -- Get start X position of the touch
else
touchID[event.id] = {}
touchID[event.id].x = event.x
xPos = "left"
pX = touchID[event.id].x
end
elseif (event.phase == "moved") then
print("ID:"..tostring(event.id))
local dX
if (xPos == "right") then
touchID[event.id].x = event.x
dX = touchID[event.id].x - pX
rotatePlayer(dx)
else
touchID[event.id].x = event.x
dX = touchID[event.id].x - pX
movePlayer(dX)
end
同樣的問題依然存在。
我嘗試將觸摸ID添加到表中,並讓它們的觸摸移動,但同樣的問題仍然存在。請檢查我更新的問題。 – Abdou023