View comments | RSS feed

Writing anonymous and callback functions

A named function is a function that you reference in your script before or after you define it, whereas an anonymous function is an unnamed function that references itself; you reference the anonymous function when you create it. When you write ActionScript code, you will create many anonymous functions.

Anonymous functions are commonly used when you work with event handlers. To write an anonymous function, you could store a function literal inside a variable. Therefore, you can reference the function later in your code. The next example shows you how to write an anonymous function.

To write an anonymous function:

  1. Create a movie clip on the Stage, and then select the clip.
  2. Open the Property inspector, and type my_mc into the Instance Name text box.
  3. Select Frame 1 of the Timeline, and type the following code into the Actions panel:
    var myWidth = function () {
        trace(my_mc._width);
    };
    //later in code you can add
    myWidth();
    
  4. Select Control > Test Movie.

    The width of the movie clip is displayed in the Output panel.

You can also create a function inside an object, such as an XML or LoadVars instance. You can associate an anonymous function with a certain event to create a callback function. A function calls a callback function after a specific event occurs, such as after something finishes loading (onLoad()) or finishes animating (onMotionFinished()).

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 onLoad() event. You use XML.load() and the callback 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 onLoad() event handler uses an anonymous function to handle the onLoad() event.

For more information on callback functions, see Handling Events.

You could also use anonymous functions with the setInterval() function, as seen in the following code, which uses setInterval() to call the anonymous function approximately 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();

Similarly, the following code placed on any frame does not work:

myWidth();
var myWidth:Function = function () {
    trace(my_mc._width);
};

However, this code works properly:

var myWidth:Function = function () {
    trace(my_mc._width);
};
myWidth();

NOTE

 

You could also place myWidth() on any frame that is after the frame that contains the myWidth function.

When defining a named function, calling it in a frame script works, even though the equivalent code with an anonymous function does not work:

// the following does work because you are calling a named function:
myWidth();
function myWidth() {
    trace("foo");
}
// the following does not work because you are calling an anonymous function:
myWidth();
var myWidth:Function = function () {
    trace("foo");
};

For more information, see Writing named functions.

NOTE

 

For information on writing code using Script Assist, see Using Flash.


Flash CS3


Comments


MarkOfTheMole said on Jul 12, 2007 at 9:31 AM :
I understand the use of trace() to simplify the code examples but unfortunately it gives no clues to issues regarding scope.

In the case of onMotionFinished it would be nice if you included a second example that explains how to get back to the scope of class file / object from where it is called without using _root or other bad medicine. There may be a very simple solution but I've been having a tough time calling a method in the class file from where the callback is instantiated and there seems to be very sparse docs related to this particular feature. Maybe someone can add that one note on scope to the onMotionFinished entry

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000751.html