简体   繁体   中英

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

I am creating an object i want it to make it draggable when i apply touch event on it, and if user touches that object for more than 5 sec then that object must not work as draggable object but then i have to call some other function, after that i want to clear the counter so that after next touch it will be reinitialized..... how can it be achieved in corona i was trying this with Timer = os.time() but could not get the perfect result. Please suggest any idea... thanks

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)

i'm assuming that you cannot get what you achieve because you did not remove the listener when going to another function see my code as a reference:

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)

you can post another snippet of your code where the problem occurs if the idea above does't work since i only assume the problem that you have encountered. its hard to point out where the problem occur if there is only a snippet of code.

The event table passed on touch events has the time the event happened. I would on the began phase, store the event.time. Then when you get your first moved phase, if the time is over 5 seconds behave one way, else behave the other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM