Using Events and Handlers

The previous exercise looked briefly at an event in Flash. Events are like flags that are raised when something happens within Flash. An event is an action that occurs at runtime in a SWF file. When the SWF file is playing, events occur when a movie clip or an XML file loads, a button is clicked, and so on. Event handlers and listeners are actions that help manage those events. You've seen a couple examples of events already with the LoadVars class. When the file has been loaded by Flash, the onLoad event is generated by Flash, and any function defined for that event is then executed. A simple example of this happening is when a button is clicked (the event): The SWF file goes to and plays a designated frame.

An example of what you've already seen is when a file is loaded, either successfully or not. LoadVars TRiggers the onLoad eventtelling Flash that the operation has completed. This is necessary because Flash continues processing code instead of halting the SWF file to wait for the operation to complete. This is what is known as asynchronous communication. In the previous exercise, you ask the SWF file to load the contents of a text file. When the SWF file receives a result, the SWF file either displays text in the TextArea component or displays an error in the Output panel (if you are in the test environment). Because trace statements are not sent to the user when you publish the FLA file, users do not see the contents of any trace statements in your SWF file.

There are all sorts of different events for many items within Flash. If you are loading an MP3 at runtime using Flash, for example, three different events can occur: onID3 TRiggers when the ID3 data (information about the MP3 file) is available, onLoad TRiggers when the MP3 has finished loading, and onSoundComplete TRiggers after the MP3 has finished playing. When something triggers, it means that Flash executes any function that might be defined in the event handler for these three events. You can see an example of this happening in the following code:

var intro_sound:Sound = new Sound(); intro_sound.loadSound("Tool - disgustipated.mp3", true); intro_sound.onID3 = function(success:Boolean) {           trace(success); }; intro_sound.onSoundComplete = function() {           trace("sound has completed"); };

This script first defines a new instance of the Sound object and then loads in an MP3 into the Sound object instance using the built-in loadSound method. The true after the MP3 filename tells the SWF file that you want to stream the MP3 rather than waiting for the entire MP3 to load before beginning playback. Then you create event handlers for the onID3 event and for the onSoundComplete event. As each of these events occurs, the SWF file executes each function that's defined in the event handler automatically. The event handler are the instructions that execute when the event occurs. That means that when any sort of ID3 information is available in the SWF file, the onID3 event triggers, the function executes, and the SWF file traces the value of the success attribute. The trace tells you whether the sound was successfully loaded into the SWF file.

When the MP3 finishes playing, the onSoundComplete event triggers, and the SWF file displays a message in the Output panel in the testing environment. The onSoundComplete function can be very useful if you want to load a new MP3 when the current MP3 finishes playing.

Macromedia Flash 8. Training from the Source
Macromedia Flash 8: Training from the Source
ISBN: 0321336291
EAN: 2147483647
Year: 2004
Pages: 230
Authors: James English © 2008-2017.
If you may any questions please contact us: