检测多点触摸
Detect Multiple Touch
在我的游戏中,我使用触摸事件控制一个对象。当我触摸屏幕的右半部分时,对象会旋转,当我触摸屏幕的左半部分时,对象会移动。单次触摸时效果很好,但是当我触摸屏幕的任何一侧然后同时开始触摸另一侧时,会导致意外的混合行为。
我想我的问题是,如何将多个触摸彼此分开或区分。
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
同样的问题仍然存在。
您似乎忽略了 event.id
字段,这就是为什么您会混淆多个触摸行为。
当您进入 began
阶段时,通过将其存储在某个列表中来跟踪每个新触摸。包括触摸初始坐标(你的 pX
去那里)和你以后可能需要的任何其他东西。当您收到其他事件 (moved/ended/cancelled) 时,您应该检查活动触摸列表,找到 event.id
的实际触摸并为该确切触摸执行逻辑。
您仍在混合触摸数据。 xPos
是一个触摸函数,因此它必须存储在触摸事件中,而不是存储在使用其他触摸数据更新的全局变量中。
此外,将重复的行移出 if
个分支,它们是相同的。代码将变得更简单,更易于阅读和理解。
system.activate( "multitouch" )
local touchID = {} --Table to hold touches
onTouch = function (event)
local x, id, phase = event.x, event.id, event.phase
print( "ID:"..tostring(id) )
if (phase == "began") then
touchID[id] = {
x = x,
logic = (x > centerX) and rotatePlayer or movePlayer
}
elseif (phase == "moved") then
local touch = touchID[id]
touch.logic(x - touch.x)
end
end
请注意,您仍应删除 "ended/cancelled" 阶段的接触。
编辑:屏幕的同一侧可能有多个触摸,因此要么忽略新的触摸,要么以某种方式平均它们。
在我的游戏中,我使用触摸事件控制一个对象。当我触摸屏幕的右半部分时,对象会旋转,当我触摸屏幕的左半部分时,对象会移动。单次触摸时效果很好,但是当我触摸屏幕的任何一侧然后同时开始触摸另一侧时,会导致意外的混合行为。
我想我的问题是,如何将多个触摸彼此分开或区分。
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
同样的问题仍然存在。
您似乎忽略了 event.id
字段,这就是为什么您会混淆多个触摸行为。
当您进入 began
阶段时,通过将其存储在某个列表中来跟踪每个新触摸。包括触摸初始坐标(你的 pX
去那里)和你以后可能需要的任何其他东西。当您收到其他事件 (moved/ended/cancelled) 时,您应该检查活动触摸列表,找到 event.id
的实际触摸并为该确切触摸执行逻辑。
您仍在混合触摸数据。 xPos
是一个触摸函数,因此它必须存储在触摸事件中,而不是存储在使用其他触摸数据更新的全局变量中。
此外,将重复的行移出 if
个分支,它们是相同的。代码将变得更简单,更易于阅读和理解。
system.activate( "multitouch" )
local touchID = {} --Table to hold touches
onTouch = function (event)
local x, id, phase = event.x, event.id, event.phase
print( "ID:"..tostring(id) )
if (phase == "began") then
touchID[id] = {
x = x,
logic = (x > centerX) and rotatePlayer or movePlayer
}
elseif (phase == "moved") then
local touch = touchID[id]
touch.logic(x - touch.x)
end
end
请注意,您仍应删除 "ended/cancelled" 阶段的接触。
编辑:屏幕的同一侧可能有多个触摸,因此要么忽略新的触摸,要么以某种方式平均它们。