Game Development Reference
If you're new to programming, you'll need a bit of practice using methods and looking at different
examples. Don't worry! No one completely understands methods at first. Just keep this chapter at
hand and experiment with some of the sample programs included in this chapter's source files. You'll
see many concrete examples of methods at work in this topic and you'll gradually feel comfortable
Understanding events and event listeners
Events are things in a program that happen. You'll be happy to let most things that happen in your
program, such as values being assigned to variables, take care of themselves without needing to be
bothered with the details. But you'll want to know about some events, such as button clicks, so that
you can specify certain directives to run when they occur.
AS3.0 allows you to attach event listeners to objects. An event listener “listens” for things that hap-
pen in the program. When an event occurs, the listener triggers instructions for what to do. You can
think of an event listener as an extremely clever little dog that loves to bark. You've trained your little
dog not only to bark madly at burglars but also to dial the number of the police station while doing
so and then bark the address of your house to the officer on the other end of the phone. Clever little
dog! And that's the kind of dog you have at your disposal with event listeners in AS3.0.
Creating event listeners is a three-step process. You need to do the following to set them up and use
them in the program:
1. Import an event class from Flash's arajpo package.
2. Add an event listener to an object using a method call to AS3.0's built-in ]``ArajpHeopajan
method. The method call includes a number of arguments, such as the kind of event to listen
for and what to do when the event occurs.
3. Create an event handler, which is a specialized function definition that tells the program what
to do when the event occurs. It “handles” the event. The event handler includes a special
parameter that allows it to accept an event object, which provides quite a bit of information
about the event that took place. You can use this information in your program.
The best way to understand how event listeners work is to see one in action. In the chapter's source
files you'll find a folder called Arajpheopajan. Open the files inside as a project, following the steps
described earlier in the chapter. When you test the project, click the stage with the left mouse but-
ton. You'll see You clicked on the stage displayed in the Output panel. Figure 3-13 illustrates what the
program looks like and the result in the Output panel.
Now that you've seen events in action, let's look at a way to make them.