When you hear the word, “event,” what do you think of? Here are some events that immediately come to mind for me:
- IPTSO letter writing party
- Theatrical play
If we were to break down the essential elements of an event, we might come up with the following:
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:
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.
- A trigger
- A listener (you were actively “listening” for that trigger)
- 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:
--action local function doThisThing(event) print(event.target.name)--show the id of the thing that was clicked end --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
<table> <tbody> <tr> <td id="td1">Hello</td> </tr> </tbody> </table>
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?