Lua 触摸事件检测问题
将事件侦听器添加到对象并移动到该对象外部时,不会触发Lua 触摸事件检测问题,lua,coronasdk,Lua,Coronasdk,将事件侦听器添加到对象并移动到该对象外部时,不会触发event.phase==“end”,因为它在对象外部检测到。 我的问题:是否有一种方法可以检测event.phase==“end”,即使用户在对象外部释放触摸,还是有其他方法可以检测用户是否在不使用运行时事件侦听器的情况下抬起手指?您可以尝试以下方法: local bg = display.newRect(0,0,display.contentWidth,display.contentHeight) local rect = display
event.phase==“end”
,因为它在对象外部检测到。
我的问题:是否有一种方法可以检测
event.phase==“end”
,即使用户在对象外部释放触摸,还是有其他方法可以检测用户是否在不使用运行时事件侦听器的情况下抬起手指?您可以尝试以下方法:
local bg = display.newRect(0,0,display.contentWidth,display.contentHeight)
local rect = display.newRect(100,200,100,100)
rect:setFillColor(0)
local isRectTouched = false;
local function bgTouch_function(e)
if(isRectTouched == true and e.phase == "ended")then
isRectTouched = false;
print("Started on Rect and ended outside")
end
end
bg:addEventListener("touch",bgTouch_function)
local function rectTouch_function(e)
if(e.phase == "began" or e.phase == "moved")then
isRectTouched = true;
print("began/moved .... rect")
else
isRectTouched = false;
print("ended .... rect")
end
end
rect:addEventListener("touch",rectTouch_function)
继续编码 您可以尝试以下方法:
local bg = display.newRect(0,0,display.contentWidth,display.contentHeight)
local rect = display.newRect(100,200,100,100)
rect:setFillColor(0)
local isRectTouched = false;
local function bgTouch_function(e)
if(isRectTouched == true and e.phase == "ended")then
isRectTouched = false;
print("Started on Rect and ended outside")
end
end
bg:addEventListener("touch",bgTouch_function)
local function rectTouch_function(e)
if(e.phase == "began" or e.phase == "moved")then
isRectTouched = true;
print("began/moved .... rect")
else
isRectTouched = false;
print("ended .... rect")
end
end
rect:addEventListener("touch",rectTouch_function)
继续编码 我建议使用内置的setfocus方法,它允许您将触摸事件绑定到特定的显示对象。这样,即使您离开对象,也可以获取事件。你可以仔细阅读这种编码方法
local function bind(event)
if event.phase=='began' then
display.getCurrentStage():setFocus(event.target)
end
if event.phase=='moved' or event.phase=='began' then
elseif event.phase=='ended' then
display.getCurrentStage():setFocus(nil)
-- Whatever you want to do on release here
end
end
我建议使用内置的setfocus方法,它允许您将触摸事件绑定到特定的显示对象。这样,即使您离开对象,也可以获取事件。你可以仔细阅读这种编码方法
local function bind(event)
if event.phase=='began' then
display.getCurrentStage():setFocus(event.target)
end
if event.phase=='moved' or event.phase=='began' then
elseif event.phase=='ended' then
display.getCurrentStage():setFocus(nil)
-- Whatever you want to do on release here
end
end