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

时间:2013-08-18 06:25:27

标签: iphone lua corona

我正在创建一个对象,我希望它可以在我对其应用触摸事件时使其可拖动,如果 用户触摸该对象超过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)

2 个答案:

答案 0 :(得分:0)

我假设你无法得到你所获得的东西,因为你在转到另一个函数时没有删除监听器,请参阅我的代码作为参考:

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)

如果上述想法不起作用,您可以发布问题的另一段代码,因为我只假设您遇到的问题。如果只有一段代码,很难指出问题出现在哪里。

答案 1 :(得分:0)

触摸事件传递的事件表具有事件发生的时间。我会在开始阶段,存储event.time。然后,当你得到你的第一个移动阶段时,如果时间超过5秒表现为单向,否则表现另一个。