如何在tap功能中对corona进行持续动作

如何在 tap 函数中持续执行针对冠状病毒的操作?我的意思是当 event.phase = "began" 并且一直到被点击时,该操作重复执行直到结束。

我的代码:

function upArrowtap(event)
  if (event.phase == "began") then
    if ( ball.y > 45 ) then
      transition.cancel(trans1)
      transition.cancel(trans2)
      --ball.y = ball.y-15
      start()
    end
  end
end

upArrow:addEventListener("touch", upArrowtap)

希望你能理解我的问题。

原文链接 https://stackoverflow.com/questions/5764354

点赞
stackoverflow用户686008
stackoverflow用户686008

首先,在“触摸”上使用事件侦听器,而不是“敲击”。敲击事件侦听器仅在手指被移除时响应,但触摸侦听器对触摸的开始和结束都响应。

其次,要重复触发事件,需要使用enterFrame。因此,在开始触摸时设置enterFrame侦听器,在触摸结束时删除enterFrame侦听器:

local function onEnterFrame(event)
  ball.y = ball.y + 2
end
local function onTouch(event)
  if (event.phase == "began") then
    Runtime:addEventListener("enterFrame", onEnterFrame)
  elseif (event.phase == "ended") then
    Runtime:removeEventListener("enterFrame", onEnterFrame)
  end
end
button:addEventListener("touch", onTouch)

(我可能有几个关键字错了,我只是凭记忆打的)

2011-04-23 13:16:17