如何在Corona SDK中根据用户触摸物体的时间选择从两个函数中调用哪个函数?

我正在创建一个对象,当我在对象上应用触摸事件时,希望能够使它可拖动,如果用户触摸该对象超过5秒,则该对象不应作为可拖动对象工作,而是我必须调用另一个函数,之后我想清除计数器,以便在下一次触摸后重新初始化计数器……如何在corona中实现这一点,我试过使用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)
点赞
用户1682268
用户1682268

我猜测你无法达成你的目标,因为当你进入另一个函数时没有移除监听器。可以参考我的代码:

local function func1()

--将对象设置为可拖动

end

local function func2()
--移除func1()的监听器并在此处设置另一个监听器
--在此处调用其他函数

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)

如果上述想法不起作用,您可以发布代码中出现问题的另一段代码。由于只有一小段代码,很难确定问题出现的地方。

2013-08-18 12:54:39
用户1870706
用户1870706

触摸事件传递的事件表具有事件发生的时间。我会在开始阶段存储事件时间。然后,当您获得第一个移动阶段时,如果时间超过5秒,则以一种方式行为,否则以另一种方式行为。

2013-08-18 21:50:49