Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Best Practices and Coding Conventions for ActionScript 2.0 > Formatting ActionScript syntax > About using listener syntax | |||
You can write listeners for events in several ways in Flash 8 and later. Some popular techniques are shown in the following code examples. The first example shows a properly formatted listener syntax, which uses a Loader component to load content into a SWF file. The progress event starts when content loads, and the complete event indicates when loading finishes.
var boxLdr:mx.controls.Loader;
var ldrListener:Object = new Object();
ldrListener.progress = function(evt:Object) {
trace("loader loading:" + Math.round(evt.target.percentLoaded) + "%");
};
ldrListener.complete = function(evt:Object) {
trace("loader complete:" + evt.target._name);
};
boxLdr.addEventListener("progress", ldrListener);
boxLdr.addEventListener("complete", ldrListener);
boxLdr.load("http://www.helpexamples.com/flash/images/image1.jpg");
A slight variation on the first example in this section is to use the handleEvent method, but this technique is slightly more cumbersome. Adobe does not recommend this technique because you must use a series of if..else statements or a switch statement to detect which event is caught.
var boxLdr:mx.controls.Loader;
var ldrListener:Object = new Object();
ldrListener.handleEvent = function(evt:Object) {
switch (evt.type) {
case "progress" :
trace("loader loading:" + Math.round(evt.target.percentLoaded) + "%");
break;
case "complete" :
trace("loader complete:" + evt.target._name);
break;
}
};
boxLdr.addEventListener("progress", ldrListener);
boxLdr.addEventListener("complete", ldrListener);
boxLdr.load("http://www.helpexamples.com/flash/images/image1.jpg");
|
|
|
|