Event listeners

 Class  Comments Off on Event listeners
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

Types, functions, and types

 Class  Comments Off on Types, functions, and types
Feb 112013
gumball machine

A function is like a gumball machine

In reality, you’ve been using functions all your life.  A function is like a gumball machine.   There are 3 parts to every function: an input, a process, and an output.  In the case of our gumball machine:

  • input: a coin
  • process:  turn the handle, the machine deposits the coin in a repository, the machine opens a door, gumballs fall through the door, the door closes.
  • output: gumballs!

What other examples of functions can you think of that you’ve seen in real life?

A function, as defined in code, might look like this:

//define a function
function secretNumber(yourNum:Int) {
var yourNewNum = 100*((yourNum*7)/58);
return yourNewNum

//now we need to call the function to use it (so far, it's just been defined, but never used)
alert("Your secret number is "+secretNumber(4));

In the above code, the input was the variable yourNum; the process was to multiply that by 7, divide the product by 5, and then multiply that quotient by 100; the output returned the new number to you.


If I asked you to put a telephone into the gumball machine, you’d think I were crazy. Why? Because it’s not the right type of input. This machine only accepts the type of object that is a coin. Computer programs work the same way. A function is expecting a particular type of data. In our secretNumber function, we can see that an integer is required. Had we tried to use a string, the function would have failed. There are many different types in programming. Some of the more common types are:

  • string

    : a series of characters (letters, numbers, symbols). Typically, they are included in either single or double quotes.

  • integer

    : a whole number (no decimals!). If you include a decimal, the number will be cut off at the decimal point (not rounded). Can be used for arithmetic operations.

  • number (or floating point number)

    :a number with decimals.

  • array

    :A list of items. An array is different depending on the language. In javascript, an array is an indexed list of items. For each index, there is a value. Let’s consider that we have an array called “months.” At index 5, the value would likely be the 6th month, June (in javascript, array indices start at 0).

  • Object

    : An object is like an array; only, instead of having a numerical index, we find the value of specific properties by referring to their keys. Suppose we had an object named Person. In javascript, we can create a property of person as a key of that object, like so:

    Person.name = "Peter"

    The key is “name” and the value of that key is “Peter”. Objects exist in many languages and are similar to other types, such as: a hash, an associative array, a table, a list, a map, a dictionary.