sheraz amin
sheraz amin

Reputation: 1149

how to do continous action on corona in tap function

How to do continuous action on corona in tap function? I mean when event.phase="began" and until its tapped the action repeats until its ended.

My code:

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)

Hope u understand my question.

Upvotes: 2

Views: 2384

Answers (1)

jhocking
jhocking

Reputation: 5577

First off, use an event listener for "touch" not "tap". Tap event listeners only respond when the finger is removed, but touch listeners respond to both the beginning and end of the touch.

Secondly, to have an event repeat over and over you need to use enterFrame. So set an enterFrame listener when the touch begins and remove the enterFrame listener when the touch ends:

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)

(I may have gotten a couple keywords wrong, I just typed that off the top of my head)

Upvotes: 8

Related Questions