简体   繁体   English

如何根据用户触摸电晕SDK中对象的时间,在一次触摸事件中从两个函数中选择要调用的函数

[英]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. 我正在创建一个对象,当我对其施加触摸事件时,我希望它使其可拖动,如果用户触摸该对象超过5秒钟,则该对象不能用作可拖动对象,但是我必须调用其他功能,之后,我想清除计数器,以便在下次触摸后将其重新初始化.....如何在电晕中实现,我尝试使用Timer = os.time()进行此操作,但无法获得理想的结果。 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. 我会在开始阶段存储event.time。 Then when you get your first moved phase, if the time is over 5 seconds behave one way, else behave the other. 然后,当您获得第一个移动阶段时,如果时间超过5秒,则采取一种方式,否则采取另一种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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