[英]How to choose which function to call from two functions on a single touch event based on the time user has touched an object in corona sdk
我正在創建一個對象,當我對其施加觸摸事件時,我希望它使其可拖動,如果用戶觸摸該對象超過5秒鍾,則該對象不能用作可拖動對象,但是我必須調用其他功能,之后,我想清除計數器,以便在下次觸摸后將其重新初始化.....如何在電暈中實現,我嘗試使用Timer = os.time()進行此操作,但無法獲得理想的結果。 請提出任何想法...謝謝
local function callfunc( event )
local phase = event.phase
if "began" == phase then
Timer = os.time()
if Timer>5 then
func1()
else
func2()
end
end
Runtime:addEventListener("touch",callfunc)
我假設您無法實現目標,因為在轉到另一個函數時沒有刪除偵聽器,請參見我的代碼作為參考:
local function func1()
--set the object as draggable
end
local function func2()
--remove the listener of the func1() and set another listener here
--call other function here
end
local function callfunc( event )
local phase = event.phase
if "began" == phase then
Timer = os.time()
if Timer>5 then
func1()
else
func2()
end
end
Runtime:addEventListener("touch",callfunc)
您可以在上面的想法不起作用的情況下,在發生問題的地方發布另一個代碼段,因為我僅假設您遇到了問題。 如果只有一小段代碼,很難指出問題出在哪里。
在觸摸事件上傳遞的事件表具有事件發生的時間。 我會在開始階段存儲event.time。 然后,當您獲得第一個移動階段時,如果時間超過5秒,則采取一種方式,否則采取另一種方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.