Feb 202013

When you hear the word, “event,” what do you think of?  Here are some events that immediately come to mind for me:

  • Birthdays
  • IPTSO letter writing party
  • Theatrical play
  • etc.

If we were to break down the essential elements of an event, we might come up with the following:

  • time
  • place
  • duration
  • activity

Just like events in the lives of people, there is such a thing as an event on a computer.  What events can you think of that occur at a specific time and place and have a specific activity on the computer?   Some of the more common events that might happen on a computer or electronic device are:

  • click
  • double-click
  • mouseDown
  • mouseUp
  • mouseOut
  • touch:began
  • touch:moving
  • touch:ended
  • etc.

The Listening Game:

To better understand the parts of an event, try the following game.  Pick an action (e.g., raising your hand, stomping your foot).  Now, suppose you had a teacher in a classroom.  When that teacher gives a specific signal, you engage in one of those actions.  For example, when the teacher asks a question, you raise your hand.  When the teacher scratches his nose, you stomp your foot.  Go ahead, play the game (it’ll be fun!).

This game demonstrates the 3 parts associated with making use of events in your coding.

  1. A trigger
  2. A listener (you were actively “listening” for that trigger)
  3. An action

So, you have some object that is actively “listening” for a specific trigger.  When that trigger occurs, you carry out some action (or function).  Let’s consider this in our code.

Events in Lua

A simple event and listener might look like this:

    local function doThisThing(event)
        print(event.target.name)--show the id of the thing that was clicked
    --create an object
    local btn = display.newRect(110,100,200,400)
    btn.name = "Larry"
   --make the object listen for the "tap" event and run the doThisThing function
   btn:addEventListener("tap", doThisThing)

There are lots of events you might listen for in the CoronaSDK, many that you might not even think about.  For example, did you know there is an “enterFrame” event?  This event is fired (i.e., it happens) every frame of your project.  So, if your are running at app at 30 frames per second, that would mean the enterFrame listener would go off 30 times every second!  The enterFrame event is a Runtime event.  Here is the documentation for all of the possible events in the CoronaSDK: http://docs.coronalabs.com/api/event/index.html


Events in Javascript

To create an event in Javascript, we need to make sure we have our HTML in place first.  Let’s create a table with some simple data in it:


[sourcecode language=”html”]




Now let’s change the content of a cell to the opposite value when you click on it. We need to tell the cells to listen for a click, check what the current value is, then change it to the other value accordingly. Ready?

<script type="text/javascript" language="javascript">// <![CDATA[
//create an "action"
 function changeVal(event){
    var thisCell = document.getElementById("td1");
    if (thisCell.firstChild.nodeValue == "Hello") {
       this.firstChild.nodeValue = "Goodbye";
    else {
       this.firstChild.nodeValue = "Hello";
 //create a listener
 var obj = document.getElementById("table1");
 obj:addEventListener("click", changeVal, false);
// ]]></script>

For a listing of all javascript events, see this reference: http://help.dottoro.com/larrqqck.php

Sorry, the comment form is closed at this time.