The Corona Labs Blog
Posted on . Written by

Detecting Screen Taps in CoronaPreviously, I showed you how to detect touches in Corona SDK using “touch” events. What you may not know is that there is a much simpler event that detects quick touches, or taps, and is useful for many different scenarios, games, quick testing, etc.

The “tap” event

Taps differ from touches in that once the finger moves, the event is cancelled—making this type of event perfect for quick touches, and especially for scenarios where you need to detect multiple quick taps (e.g. double-taps on a specific object or the screen).

Event Listener

Before you add an event listener to an object (or the global Runtime object), you should first decide what you want to happen when the tap takes place. As with other events, this is where an event listener function comes in. Here’s a simple one:


local function onImageTap( self, event )
print( self.id .. " was tapped." )
end

Next, you can add a “tap” event listener to an object:


local object = display.newImage( "image.png" )
object.id = "myObject1"
object.touch = onImageTap -- the function we created previously
object:addEventListener( "tap" )

And here’s how you could do the same, but detecting taps on the entire screen instead:


local function onScreenTap( event )
print( "The screen was tapped." )
end
Runtime:addEventListener( "tap", onScreenTap )

If you’ve worked with touch events at all, the first thing you’ll notice is that the listener function is much simpler. There is no checking for event phases, focus, or return values—very straight-forward. This makes tap event ideal for prototyping touch events, so you can quickly test out actions that should occur when something is touched (before converting the listener/event into a normal “touch” event).

event.numTaps

What really sets tap events apart from touch events, and will save you a lot of effort implementing from scratch using normal touch events is the ability to know how many times the user tapped via the event.numTaps property.

This is generally 1 or 2, but can sometimes be 3 if the user taps fast enough. This is most commonly used for detecting double-tap scenarios. For instance, if you have an object that needs to do something only when double-tapped, you could utilize event.numTaps like so:


local object = display.newImage( "image.png" )

function object:tap( event )
if (event.numTaps >= 2 ) then
print( "The object was double-tapped." )
end
end

object:addEventListener( "tap" )

Also, it’s worth noting that you also get event.x and event.y properties with tap events, which correspond to the x/y screen coordinates that the tap took place.

For more information on tap events, please see the official tap event documentation.

14 Responses to “Detecting Object and Screen Taps”

  1. Chris Leyton

    A nice heads up – but I’d really like to see the Beebe’ster (sorry) tackle some heavier subjects in the future.

    Seen a few forum posts about confusion with OO in Lua, particularly with the numerous frameworks around (OWL, DMC, etc…), how about it Jon? Perhaps a whole blown working example – or is that outside the remit of these blog tutorials?

    Reply
  2. Ed Schueler

    Hi Jonathan,

    Thanks for another informative tip! I have a clarifying question though. Would your double-tap code example above need to set system.setTapDelay() in order to recognize multiple taps? The Corona Doc for system.setTapDelay() seems to say this.

    Reply
  3. Andy Whittock

    sooo… in a game i want to fire a weapon when the player taps once,
    and, say jump when they tap twice.
    in the double tap case, there seems to be two events, one for the first tap with a tapcount of 1, and then another with a tapcount of 2…. that means i get a weapon fire and the jump whenever they double tap.
    does that make sense to anyone?
    is there a way to trap that?

    Reply
    • David Condolora

      Hi Andy,

      It looks like you would do it with if statements. For example:

      function object:tap( event )

      if (event.numTaps >= 2 ) then
      print( “The object was double-tapped.” )
      end
      end

      object:addEventListener( “tap” )

      Reply
    • David Condolora

      Hi Andy,

      It looks like you would do it with if statements. For example:

      function object:tap( event )
      if (event.numTaps >= 2 ) then
      — Jump
      else
      — Fire gun
      end
      end

      object:addEventListener( “tap” )

      Would that work?

      Reply
      • Michael

        i know it’s a bit late, but it might be useful if someone is browsing the comments with the same problem.

        of course your function is not wrong, but as long as you don’t change the systems tap delay to anything else than 0 (what is the default) the first tap will always be triggered as well. in Andys case that would mean, there would be still jumping and firing at (almost) the same time. (actually a tiny amount of milliseconds apart)

        i use myTapDelay to store the delay in seconds (of course you don’t need an extra variable for that, i just did it for more clearity).
        in this case it’s 0.1 sec, so 100ms.
        That means ALL TAPS will only be triggered 100ms after the interaction,
        so your app got 100ms to decide wheter it was a single or doubletap.
        But be aware that the user should be able to do the amount of required taps within the delay’s timeframe.
        And be awared that your singletap will have a delay as well.
        so here is the code:
        ——————————————
        local myTapDelay = 0.1
        system.setTapDelay( myTapDelay )
        ——————————————
        –and now David’s code
        function object:tap( event )
        if (event.numTaps >= 2 ) then
        – Jump
        else
        – Fire gun
        end
        end

        object:addEventListener( “tap” )
        ——————————————

        Reply
  4. mick

    Yep thanks again, Jonathan

    Alway’s enjoy ur posts, noob comment if i may. Should
    object.touch = onImageTap — the function we created previously
    be
    object.tap = onImageTap — the function we created previously

    Reply
  5. raj s

    how do you apply it to the object… say if you want an object to move upwards by tapping the screen.. because i already have gravity applied to it.

    Reply
  6. Brent Sorrentino

    Hi raj s,
    I’m not sure I understand your scenario. Do you want to affect an object specifically by tapping on it? Or by tapping anywhere on the screen? What do you want to happen when the tap occurs? Move the body with some kind of upward impulse?

    Brent

    Reply
  7. stuart warren

    How can you handle tapping an object, and also offer the option of double tapping it for different results? In my experiments, it always falls to the tap and never waits for the second tap

    Reply

Leave a Reply

  • (Will Not Be Published)