2
votes

I don't exactly understand what's the difference between tap and touch in Corona. I use both of them and when one touches on an object both listened for event until I wrote this piece of code which changes image when nextButton is touched. It's like when I touched the nextButton, It calls the function two times. However when I change it to tap, it worked smoothly. So can you tell me what is the difference between touch and tap and what was causing trouble when I use touch in this piece of code?

 function nextButton:touch(event)
   if i==7 then 
   else
    i=i+1   
    imageObject:removeSelf()    
    imageObject =display.newImage(imageTable[i]..".jpg")
    imageObject.x=_W/2
    imageObject.y=_H/2 
   end
 end

 nextButton:addEventListener("touch", nextButton)
2

2 Answers

3
votes

In the corona touch listeners you have 3 state:

   function listener(event)

     if event.phase == "began" then
   -- in the 1st tap the phase will be "began"
       elseif event.phase == "moved" then
   -- after began phase when the listener will be called with moved phase,like          touched coordinates
       elseif event.phase == "ended" then
       --when the touch will end 
     end


   end

    nextButton:addEventListener("touch", listener)

--[[ this was a simple touch listener for an image, self made button etc, btw when you need to yous buttons, use the ui library what is made exactly for this http://developer.coronalabs.com/code/enhanced-ui-library-uilua ]]

    -- example for usage
    local ui = require "ui" -- copy the ui.lua to your apps root directory

   yourButton = ui.newButton{
    defaultSrc = "menu/icon-back.png",--unpressed state image 
    x=85,
        y=display.contentHeight-50,
    defaultX = 110,
    defaultY =80,
    offset=-5,


    overSrc = "menu/icon-back-2.png",--pressed state image
    overX = 110,
    overY = 80,
    onEvent = buttonhandler,
    id = "yourBtnId" -- what you want
    }

      local function buttonhandler(event)

         if event.phase == "release" then

          --if you have more buttons handle it whit they id-s
            if event.id == "yourBtnId" then
              -- when your button was finally released (like in 1st example ended, but this will be called only if the release target is your button)
            end 

         end
      end
1
votes

A "tap" is a brief touch and release action. A touch can be a touch, move then release, or a touch and hold etc. A tap event simplifies your code because you only get one event: the tap happened. You don't have to code for all of the touch states.

A typical tap handler would look like:

local function tapHandler(event)
    -- do stuff
    return true
end

where has a touch handler that does the exact same thing would look like:

local function touchHandler(event) if event.phase == "ended" then -- do stuff end return true end