Events in ActionScript3

Events are things that happen. In ActionScript, some of the events that can occur are clicking or moving the mouse, typing a key on the keyboard, or things like a sound finishes playing.

If we want to execute some statements when one of these events occurs, we have to tell Flash to "listen" for that event. We do this by adding a listener. We tell it what event to listen for, and what function to execute when that event occurs.

One important event that we often want to listen for is when the user clicks the mouse. Get Adobe Flash player

In this movie we have a spinning ball and a button to stop and a button to play. We need to "listen" for the user to click one of the buttons.

  1. Start a new Flash movie.
  2. Draw a ball and make it a movie clip. Use the motion presets to make the ball spin. (Open Window, Motion Presets and select and apply Spiral-3D)
  3. Add a layer button s with two buttons and name them btnStop and btnGo. (We used the playback rounded buttons rounded green pause and rounded green play.)
  4. Add a layer at the top and name it actionscript:
    timeline
  5. Write the code as shown below:
btnStop.addEventListener(MouseEvent.CLICK, stopSpin);
function stopSpin(e:MouseEvent):void {
	stop();
} //stopSpin

The first line tells flash to listen, or pay attention if the user clicks btnStop. When that event occurs we want to call or execute a function named stopSpin.
If we tell Flash to execute a function named stopSpin, then we must also write a function by that name.

Functions

When we write a function, we start with the keyword function followed by a space and the name of the function.

Next there are parentheses with any arguments, or information, that the function is going to receive. A function that is called when the mouse clicks will receive a packet of information about that event: which button was clicked, where, was the user holding down shift when they clicked, and other information. Even though we are not using that information in this movie, we must write the function to receive that information. We must give that packet of information a name. Most programmers name this packet of information about an event e.

After the name there is a colon followed by the type (MouseEvent). The type must match the event that the function is responding to.

After closing the parenthesis around the arguments there is a colon followed by the type of information that the function will return. If the function does not return anything we use void.


The statements that will be executed are called the body of the function. These statements are enclosed in braces { }
In this movie there is just one statement: stop();

Notice that the name of the event in the first line must match the word (type) after the e in the function, both underlined in red.
The name of the function that we tell Flash to call in the first line must match the name of the function, both underlined in red.

matching code

Test the movie if the movie works, add the code to play(); when we click btnGo:

btnGo.addEventListener(MouseEvent.CLICK, startSpin);
function startSpin(e:MouseEvent):void {
	play();
} //startSpin

NEXT: Buttons for Navigation