MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH Manual page 209

Learning actionscript 2.0 in flash
Table of Contents

Advertisement

For example, sometimes you need to write ActionScript to handle data that loads into a SWF
file from the server. After you finish loading data into a SWF file, you can access the data from
that location. It's important to use ActionScript to check whether the data has been fully
loaded. You can use callback functions to send a signal that the data has been loaded into the
document.
In the following callback function, in which you load a remote XML document, you associate
an anonymous function with the
function, as shown in the following example. Type the following code on Frame 1 of
the Timeline:
var my_xml:XML = new XML();
my_xml.onLoad = function(success:Boolean):Void {
trace(success);
};
my_xml.load("http://www.helpexamples.com/crossdomain.xml");
You can see from the previous code snippet that the
anonymous function to handle the
For more information on callback functions, see
page
329.
You could also use anonymous functions with the
following code, which uses
every 1000 milliseconds (1 second):
setInterval(function() {trace("interval");}, 1000);
You can use named functions instead of anonymous functions. Named functions are often
easier to read and understand (except in some circumstances, such as callback functions). You
can also forward-reference a named function, which means you reference it before the
function exists on a timeline.
You cannot reference an anonymous function anywhere in your code (unless you assign it to a
variable), as you can when you use a named function. For example, suppose that you have
anonymous functions on Frame 5 of your FLA file, such as the following:
//with a movie clip called my_mc that spans the timeline
stop();
var myWidth = function () {
trace(my_mc._width);
};
If you place the following code on Frame 1, it cannot reference the function:
myWidth();
event. You use
onLoad()
event.
onLoad()
to call the anonymous function approximately
setInterval()
XML.load()
event handler uses an
onLoad()
Chapter 10, "Handling Events," on
function, as seen in the
setInterval()
About functions and methods
and the callback
209

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flash 8

Table of Contents