2013-06-21 103 views
1

當您將事件偵聽器添加到對象並移出該對象時,event.phase == "ended"將不會被觸發,因爲它在對象外檢測到。 我的問題:有沒有一種方法可以檢測到event.phase == "ended",即使用戶在對象之外釋放觸摸,或者有其他方式可以檢測用戶是否已經提起手指而不使用運行時事件偵聽器?觸摸事件檢測問題

回答

3

你可以試試下面的方法:

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) 

保持編碼..................

+0

謝謝你的想法:) – DevfaR

2

我會建議使用內置的SETFOCUS方法,它將允許您將觸摸事件綁定到特定的顯示對象。即使您離開物體,也可以讓事件發生。你可以閱讀這個方法here快樂編碼。

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