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...

17 Responses to “The Corona Event Model explained”

  1. BTLJ

    Thanks for the method 2 explanation, I had been meaning to post on the forum to ask how “self” technically got passed to the function (“event.target” is the key) !

    For method 3, is it the case that this method requires a function for each myImage (i.e. if there is myImage1, myImage2, etc), whereas method 2 provides for the option to share a single function for multiple myImage1, myImage2, etc?

    Reply
  2. Jonathan Beebe

    @BTLJ: Yup, you’re exactly correct. Method 3 is usually used in conjunction with specific objects, or at least, specific types of objects created with a specific function. It is useful if you have a single object that you simply want to monitor touch events (or any other kind of event for that matter).

    Glad the article was of some help :-)

    Stay tuned… more to come!

    Reply
  3. Mark

    At last! A blog post that talks about something technical… It’s been a long time!!

    Reply
  4. Neil

    Jonathan – great post for us beginners put in simple, understandable language. I agree with Piyush – a similar expanation for Tables would be helpful as well. Also, although it may be outside the scope of Corona, maybe something about preparing digital assets for your app.

    Thanks.

    Reply
  5. BTLJ

    Just to make things absolutely crystal clear, in method 2, where: myImage.touch = touchListener, is it the case that:
    1) myImage.touch means the field “touch” of object “myImage”;
    2) “.touch” is reserved for tables to receive touch events from a listener?

    Hence, we are first defining the field myImage.touch = touchListener (a function within the table myImage), and then subsequently when we call the event listener it knows to refer to myImage.touch

    And as touchListener is a object/table entry within myImage, “self” therefore refers to myImage?

    Sorry for the complexity but I want to nail down conceptually whats happening step-by-step (I understand this all works but just want to fully confirm how technically it works)

    Thanks

    Reply
  6. Jonathan Beebe

    @BTLJ –

    1) myImage.touch should be defined as a function, or a reference to a separate function (which will act as the touch listener), however, this is only if you call addEventListener(). If you don’t call addEventListener, then myImage.touch becomes just like any other property you set.

    2) .touch is not necessarily reserved, it’s just that when you call addEventListener, if you are setting up a “touch” listener, if you specify the object itself as the listener, then it will search for a .touch property (because it is the name of the event).

    It’s not reserved though, you can put whatever you want in the .touch property of a variable and NOT assign a touch listener to it if you don’t want.

    This statement you made:

    “Hence, we are first defining the field myImage.touch = touchListener (a function within the table myImage), and then subsequently when we call the event listener it knows to refer to myImage.touch”

    … is correct, however, when you call addEventListener, it’ll only refer to myImage.touch IF you specify the object itself as the listener. If you specify a separate function (and not the object), it’ll use whatever function you specified instead as the listener.

    Reply
  7. BTLJ

    Jonathan, thanks, I think your explanation is the first time these specifics have been covered to this level of detail – v helpful !

    Reply
  8. Peter

    Just wondering If I can create ANY event, not just “touch” events.
    Ie, I create an event listener for event X and I send event X, so only the objects that have the X listener will listen

    Reply
  9. Kang Ghee Keong

    Very clear explanation. Also touches on key aspects. Well done and thanks for the effort!

    Reply
  10. Mo

    Hi Jon.

    Great stuff as usual!

    I just decided to switch from a virtual joystick to a touch control in my game and can see I have a lot to learn still! Anyway my question has to do with what if you have multiple sprites moving around on the screen and you want to tap them to kill them?

    I will assume you will have a table of sprites and for each sprite ( inside a for loop for instance) you will need to add a touch event listener. To that point I go it BUT how do you remove a sprite when touched? You need to use removeself I suppose. Would that remove the specific sprite or do you need to remove it manually from the orginal sprites table as well? ( ie sprites[i] = nil and so on) and what happened if you have a move sprites function that move all sprites in sequence ( again a for loop)? Would that loop would know to skip the sprites (i) element that you just destroyed by touching it?

    I am sure my questions are probably stupid but I cannot seems to figure out…. I probably should just go for a walk and think ah ah!

    Once again I just LOVE your blog posts and the way you explain things!

    Thanks Jon.

    Mo

    Reply
  11. Alex

    Hi
    I am trying to run the following code but it gives me an error, what am I doing wrong?

    local function mapAddressHandler( event )
    — handle mapAddress event here
    print( “The specified location is in: ” .. event.city .. “, ” .. event.country )
    end

    myMap:nearestAddress( 38.898748, -77.037684 )

    Runtime:addEventListener( “mapAddress”, mapAddressHandler )

    The error is “myMap” first which is the object but when I substitute it with the name of a button then it says: “attempt to call a method ‘nearestAddress’ (a nil value)”

    Reply
  12. Lynne Bolduc

    Hi Jonathan,

    I’m not a programmer by trade. This is brilliantly clear and extremely helpful. Thanks so much.

    Can you confirm/explain how return true can be used to stop propagation of the touch event for stacked elements, so that only the first-touched element receives the touch (not the bottom one)?

    Thank you,

    Lynne

    Reply

Leave a Reply

  • (Will Not Be Published)