ActionScript3:Code Templates

Introduction
Use the following Code Templates in your own AS3 Movie

Using the .addEventListener
An ActionScript Event is any interaction within Flash, from mouse clicking, loading a new object to moving along the timeline. ActionScript 3 uses an Event Listener to listen for an event - such as a mouse click - and causes an action on a target. This is achieved by using the  method. This method registers the mouse click so the target knows where to send the event object:



Image modeled on one found in the book - Flash CS5 The Missing Manual

ActionScript 3 has changed from ActionScript 2 in that you no longer apply the code directly to the object, now all code goes on the timeline.

The format is shown here:

Using the  method is the backbone of interactivity with an ActionScript 3 movie.

Using the .removeEventListener
While the  looks for an event to occur and causes a reaction, the   method can remove the reaction based on whatever conditions you apply.

The format is shown here:

A good example of this is using the add and remove methods to toggle a button event, eg. Clicking a button reveals and image, click again removes the image. Use IF statements to set the conditions. Set the button text to dynamically change as well within the same functions to "Show" "Hide" or some similar text with meaning to whatever scenario you are using.

For a small example using the .addEventListener and .removeEventListener to toggle a button event go to this small tutorial Using EventListeners to change a Button Event

Loading an external swf
ActionScript 3 no longer supports  to load an external swf into the current window. Use this code to achieve the same result: HOT TIP: Be careful with then naming of any object you have in the library. If an object in the main movie has the same name as an object in the swf you are loading, the object in the first movie will be used. Keep naming unique across all movies you intend to join together.

Using  sets the z index of the new movie. In this case at level 1, or directly on top of the current movie. You can load at a lower level, but the new swf will remain hidden. You can also load into an empty movieClip positioned anywhere on the stage by appending  to the movieclip name:

This actionscript can be used to load external images as well. All other script remains the same.

A variation of the above, giving the x and y coordinates to load the external swf:

Launching a swf inside a browser with the click of a button
Insert the following ActionScript on the frame one of the actions layer timeline within your movie. If you have the button inside another movieclip, you will need to insert this ActionScript on frame one of the movieclips timeline, not the main timeline. Just rename  to match the name you have given the instance of your own button,   is the function name and can be anything appropriate, likewise. Replace target.swf with you own swf name. will open the movie in the same browser window, remove if you want a new window to open.

Moving along the timeline
Moving along the timeline differs little from ActionScript 2. The only thing to be aware of is that any instance of an object that you have called to the stage via ActionScript will remain there unless you get rid of it - don't expect an object to be gone just because you have inserted a new keyframe or blanked the stage. The secret is to use the  or   methods shown below. To advance along the timeline insert the following ActionScript - In this case using a button to initiate the  to a frame label. ActionScript 3 requires in a lot of instances, that you use labels instead of frame numbers.

Removing Children
Any instance of an object you have called to the stage via ActionScript remains there until you use either the  or   methods to get rid of it. lets you specify the instances name, and  lets you specify the level the object is at. In this example a button will return the user to a start page, and remove an object at level 1 and also an object called :

If the object is on the maintimeline and you are trying to remove it say from a button within another movieClip on stage, you can try this:

Advancing to another Scene
According to quite a number of Flash Tutorial sites, its no longer good practice to uses Scenes within a Flash movie. But if you do decide to go with it, here's the code to advance to a new scene, using a button click event:

It is important to use a frame label in this case, not a frame number.

Mouse Events
Mouse Events allow Flash to react to a variety of mouse actions taken by the user. Below is a small list of events that are available in ActionScript 3.

Use the following code sample to attach the event listener to an object:

Separating your ActionScript from your FLA file
If you would like to store all your ActionScript in an Actionscript (.as) file, create a new file - File > New > ActionScript file. A new window opens, very similar to the Actions panel within the movie itself.

You can now write all your ActionScript in here, and attach it to you movie by entering the following code in frame One of your main movie:

Garbage Collection
Garbage collection is a means by which Flash Player purges from memory objects that you no longer need. (Shupe, 2007). There are several optional parameters that you can add to the end of the  method, one of which facilitates garbage collection. The syntax is as follows:

allows you to handle the listener once it reaches its target if its set to false - which is the default and will unlikely need to change from this.

allows you to order the execution of multiple listeners set to respond to the same event in the same phase. Set to 0 in the majority of cases.

is the memory management option. Set this to true, so that when you are no longer referencing an object, the garbage collector knows it is marked for disposal and will purge it from memory.

The  syntax would now read:

This is the recommended optional parameters for Event Listeners (Shupe, 2007).

Resources

 * Adobe Flash Platform - Loading an external Swf


 * Edutech Wiki - ActionScript 3 Event Handling