Methods

A method is an action that can be performed by an object. For example, if you've made a movie clip symbol in Flash with several keyframes and animation on its timeline, that movie clip can play, or stop, or be instructed to move the playhead to a particular frame.

This code instructs the MovieClip named shortFilm to start playing:

shortFilm.play();

This line makes the MovieClip named shortFilm stop playing (the playhead stops in place, like pausing a video):

shortFilm.stop();

This code makes a MovieClip named shortFilm move its playhead to Frame 1 and stop playing (like rewinding a video):

shortFilm.gotoAndStop(1);

As you can see, methods, like properties, are accessed by writing the object's name (a variable), then a period, and then the name of the method followed by parentheses. The parentheses are the way that you indicate that you're calling the method--or in other words, instructing the object to perform that action. Sometimes values (or variables) are placed in the parentheses, as a way to pass along additional information that is needed to carry out the action. These values are known as method parameters. For example, the gotoAndStop() method needs to know which frame it should go to, so it requires a single parameter in the parentheses. Other methods, like play() and stop(), are self-explanatory, so they don't require extra information. Nevertheless, they are still written with parentheses.

Unlike properties (and variables), methods aren't used as value placeholders. However, some methods can perform calculations and return a result that can be used like a variable. For example, the Number class's toString() method converts the numeric value to its text representation:

var numericData:Number = 9;
var textData:String = numericData.toString();

For instance, you would use the toString() method if you wanted to display the value of a Number variable in a text field on the screen. The TextField class's text property (which represents the actual text content displayed on the screen) is defined as a String, so it can contain only text values. This line of code converts the numeric value in the variable numericData to text, and then makes it show up on the screen in the TextField object named calculatorDisplay:

calculatorDisplay.text = numericData.toString();

Flash CS3


 

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

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