Posted on by
NOTE: This tutorial is outdated and has been replaced by the Basic Interactivity and Event Detection guide. Please refer to this guide for details and usage examples.

Posted by . Thanks for reading...

20 Responses to “Tutorial: Detecting Touches in Corona”

  1. Joel

    This is a great article that clearly explains how to handle touch events. Thanks so much!

    Reply
  2. tetu

    aren’t all 3 listeners identical?

    is there something that listener A (or B) can do but a function listener cannot?

    Reply
  3. Lynne

    Jonathan, if there’s a functional difference, can you please clarify usage? When would you choose A/B (table) over function? and, subsequently, A over B? Thanks.

    Reply
  4. Simon Strange

    Excellent as always Jonathan.

    Something very important, which really tripped me up, is the fact that different Android devices fire the event.phase = “moved” very differently. Some Android devices behave like iOS – they do not fire a “moved” phase unless the finger moves a few px across the screen. But some Android devices (XS is one, Kindle Fire as well) fire “moved” phases if any change at all occurs during the touch. This makes debugging some touch events very tricky!

    I initially used “tap” touch events for my menu presses, but I didn’t like some of the timing parameters for what constitutes a “tap” on iOS, so I rolled my own tap-detector using event.phase. Unfortunately it broke horribly on about 30% of Android devices – my testers couldn’t access any menus, except maybe 1 in 40 attempts! Eventually I split my code so that Android uses “tap” events, while iOS uses my home-rolled solution. But it certainly demonstrated to me why “tap” was an important concept for clean cross-platform development!

    Reply
  5. All

    THaaaaaaaaaaaaaaaaaaaaNKssssssssssssssssssssssssss

    THaaaaaaaaaaaaaaaaaaaaNKssssssssssssssssssssssssss

    THaaaaaaaaaaaaaaaaaaaaNKssssssssssssssssssssssssss

    THaaaaaaaaaaaaaaaaaaaaNKssssssssssssssssssssssssss

    Reply
  6. Clueless

    Hi, I seem to be having a problem with self in the event function, but if I take self out and reference the object it works fine?

    Reply
  7. Kyle

    @tetu, @Lynne – I believe the table/function listeners are pretty much identical EXCEPT that you have no access to “self” in the function listener. Access to self can be useful if you don’t want to bother with the event object (e.g. finding the target via event, etc.).

    Reply
  8. tetu

    @Kyle
    you have access to ‘self’ by using event.target in the function listener

    if you set t=event.target in the function listener, then self.x=100 of the table listener is the same with t.x=100 of the function listener, self.isFocus = true the same with t.isFocus = true and so on

    Reply
  9. Emanouel

    thanks for the great post,

    i was wondering if someone could help me with detecting different touches in a storyboard. as I’m having difficulty regarding coding it. here is what i have so far

    ———————————————————————————
    – Covered Purch (100) Room 1
    ———————————————————————————

    local storyboard = require( “storyboard” )
    local scene = storyboard.newScene()

    ———————————————————————————
    – BEGINNING OF YOUR IMPLEMENTATION
    ———————————————————————————

    local image, text1, text2, text3, object1

    – Touch event listener for background image
    local function onSceneTouch( self, event )
    if event.phase == “began” then

    storyboard.gotoScene( “scene2″, “fade”, 400 )

    return true
    end
    end

    – Called when the scene’s view does not exist:
    function scene:createScene( event )
    local screenGroup = self.view

    image = display.newImage( “assets/graphics/Room1.png” )
    screenGroup:insert( image )

    image.touch = onSceneTouch

    object1 = display.newImage( “assets/graphics/object.png”, 175, 200 )
    screenGroup:insert( object1 )

    object1.touch = onSceneTouch

    text1 = display.newText( “Scene 1″, 0, 0, native.systemFontBold, 24 )
    text1:setTextColor( 255 )
    text1:setReferencePoint( display.CenterReferencePoint )
    text1.x, text1.y = display.contentWidth * 0.5, 50
    screenGroup:insert( text1 )

    text2 = display.newText( “MemUsage: “, 0, 0, native.systemFont, 16 )
    text2:setTextColor( 255 )
    text2:setReferencePoint( display.CenterReferencePoint )
    text2.x, text2.y = display.contentWidth * 0.5, display.contentHeight * 0.5
    screenGroup:insert( text2 )

    text3 = display.newText( “Touch to continue.”, 0, 0, native.systemFontBold, 18 )
    text3:setTextColor( 255 ); text3.isVisible = false
    text3:setReferencePoint( display.CenterReferencePoint )
    text3.x, text3.y = display.contentWidth * 0.5, display.contentHeight – 100
    screenGroup:insert( text3 )

    print( “n1: createScene event”)
    end

    – Called immediately after scene has moved onscreen:
    function scene:enterScene( event )

    print( “1: enterScene event” )

    – remove previous scene’s view
    storyboard.purgeScene( “scene0″ )

    – Update Lua memory text display
    local showMem = function()
    image:addEventListener( “touch”, image )
    text3.isVisible = true
    text2.text = text2.text .. collectgarbage(“count”)/1000 .. “MB”
    text2.x = display.contentWidth * 0.5
    end
    local memTimer = timer.performWithDelay( 1000, showMem, 1 )
    end

    – Called when scene is about to move offscreen:
    function scene:exitScene( event )

    print( “1: exitScene event” )

    – remove touch listener for image
    image:removeEventListener( “touch”, image )

    – reset label text
    text2.text = “MemUsage: ”
    end

    – Called prior to the removal of scene’s “view” (display group)
    function scene:destroyScene( event )

    print( “((destroying scene 1′s view))” )
    end

    ———————————————————————————
    – END OF YOUR IMPLEMENTATION
    ———————————————————————————

    – “createScene” event is dispatched if scene’s view does not exist
    scene:addEventListener( “createScene”, scene )

    – “enterScene” event is dispatched whenever scene transition has finished
    scene:addEventListener( “enterScene”, scene )

    – “exitScene” event is dispatched before next scene’s transition begins
    scene:addEventListener( “exitScene”, scene )

    – “destroyScene” event is dispatched before view is unloaded, which can be
    – automatically unloaded in low memory situations, or explicitly via a call to
    – storyboard.purgeScene() or storyboard.removeScene().
    scene:addEventListener( “destroyScene”, scene )

    ———————————————————————————

    return scene

    Reply
  10. Chris Palmer

    Jonathan,

    Thanks for posting this. Like tetu and Lynne, I am also curious to know if there are differences in functionality between the two types of table listeners and the function listener.

    I am fairly new to all of this, and now realize that I have been using the function listener (as this is generally the technique used in the Cheetomoskeeto video tutorials). I believe this is what has me so turned around trying to use the Storyboard template which, if I am correct, uses the table listener B method.

    Would the Storyboard sample would work just as well using the other methods, or is there was a compelling reason for selecting that particular method?

    Many thanks!

    Chris

    Reply
  11. Jonathan Beebe

    Hi all — because of Lua’s inherent flexibility, there ends up being several different ways to do the same thing.

    There is no benefit to using one method over the other, it’s all a matter of use preference. I personally prefer method B, but you might prefer another method. I wanted to show you the different ways to do it because if I showed you only Method B, then if you saw someone else (or another sample) use Method A or even a function listener, then you’d probably be confused as to what’s going on.

    But know that you can use whichever method you want (table listener A/B or function listeners) and accomplish the same thing :-)

    There is one caveat: If the listener does not provide an event.target, then table listeners are much more useful because you get a ‘self’ object to work with. For function listeners that do have an event.target, then there is no benefit to using table listeners in that case.

    Reply
  12. Chris Palmer

    Jonathan,

    Thanks so much for the clarification! Just one more newbie question (yeah right, huh?) – would you mind giving an example of a listener that would not have an event.target?

    Thanks so much!

    Chris

    Reply
  13. James White

    Jonathan,
    thanks for posting this tutorial on Touch events. I’m having a problem with Touch events using the newTableView widget. When I have newTableView under another one, in other words stacked on top of each other, the bottom newTableView gets all the Touch events and the one on top gets only the Press event. I only want the object I’m touching to get the events

    Reply
  14. blagino

    i know this isn’t what we were talking about but i got a error in my game and plz if you know what it’s can you help me.

    function moveballoon(event)
    local balloon = event.target
    balloon:applyLinearImpulse(event.x, event.y , 0, -0.05 )
    end

    balloon:addEventListener(“touch”, moveballoon) <—- it says
    "…\users\admin\document\corona projects\game\main.lua:42: in main chunk"

    Reply
  15. blagino

    i know this isn’t what we were talking about but i got a error in my game and plz if you know what it’s can you help me.

    function moveballoon(event)
    local balloon = event.target
    balloon:applyLinearImpulse(event.x, event.y , 0, -0.05 )
    end

    balloon:addEventListener(“touch”, moveballoon) <—- it says:
    "…\users\admin\document\corona projects\game\main.lua:42: in main chunk"

    Reply

Leave a Reply

  • (Will Not Be Published)