336
Handling Events
3.
Add the following code to Frame 1 of the main Timeline;
System.security.allowDomain("http://www.helpexamples.com");
var loaderListener:Object = new Object();
loaderListener.progress = function(evt_obj:Object):Void {
trace(evt_obj.type); // progress
trace("\t" + evt_obj.target.bytes " of " +
evt_obj.target.byte " bytes loaded");
}
loaderListener.complete = function(evt_obj:Object):Void {
trace(evt_obj.type); // complete
}
my_ldr.addEventListener("progress", loaderListener);
my_ldr.addEventListener("complete", loaderListener);
my_ldr.load("http://www.helpexamples.com/flash/images/image1.jpg");
This ActionScript code defines a listener object named
loaderListener
, which listens for
two events:
progress
and
complete
. When each of these events are dispatched, their
code is executed, and debugging text is displayed in the Output panel if you test the SWF
file in the authoring tool.
Next you tell the
my_ldr
instance to listen for each of the two specified events (
progress
and
complete
) and specify the listener object or function to execute when the event is
dispatched. Finally, the
Loader.load()
method is called, which triggers the image to
begin downloading.
4.
Select Control > Test Movie to test the SWF file.
The image downloads into the Loader instance on the Stage, and then several messages are
displayed in the Output panel. Depending on the size of the image you download, and if
the image was cached on the user’s local system, the
progress
event might be dispatched
numerous times, whereas the complete event is only dispatched after the image is
completely downloaded.
When you work with components and dispatch events, the syntax is slightly different
from the event listeners in previous examples. Most notably, you must use the
addEventListener()
method instead of calling
addListener()
. Secondly, you must
specify the specific event you want to listen for as well as the event listener object
or function.
Summary of Contents for FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Page 1: ...Learning ActionScript 2 0 in Flash...
Page 8: ...8 Contents...
Page 18: ...18 Introduction...
Page 30: ...30 What s New in Flash 8 ActionScript...
Page 66: ...66 Writing and Editing ActionScript 2 0...
Page 328: ...328 Interfaces...
Page 350: ...350 Handling Events...
Page 590: ...590 Creating Interaction with ActionScript...
Page 710: ...710 Understanding Security...
Page 730: ...730 Debugging Applications...
Page 780: ...780 Deprecated Flash 4 operators...
Page 830: ...830 Index...