Search code examples
touchcoronasdktap

Difference between touch and tap in Corona


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)

Solution

  • 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