Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Functions and Methods > About functions and methods > Using functions in Flash | |||
This section shows you how to use functions in an application. Some of the following code examples use ActionScript that resides in the FLA file, and other code examples place functions in a class file for comparison. For more information and examples on using functions in a class file, see Classes. For detailed information and instruction on how to write functions for a class file, see Example: Writing custom classes.
To reduce the amount of work you have to do, as well as the size of your SWF file, try to reuse blocks of code whenever possible. One way you can reuse code is by calling a function multiple times instead of creating different code each time. Functions can be generic pieces of code; you can use the same blocks of code for slightly different purposes in a SWF file. Reusing code lets you create efficient applications and minimizes the ActionScript code that you must write, which reduces development time.
You can create functions in a FLA file or a class file or write ActionScript code that resides in a code-based component. The following examples show you how to create functions on a timeline and in a class file.
|
TIP |
|
By packing your code into class files or code-based components, you can easily share, distribute, or reuse blocks of code. Users can install your component, drag it onto the Stage, and use the code that you store in the file, such as the workflow for code-based components available in Flash (Window > Common Libraries > Classes). |
The following example shows you how to create and call a function in a FLA file.
function helloWorld(){
// statements here
trace("Hello world!");
};
This ActionScript defines the (user-defined, named) function called helloWorld(). If you test your SWF file at this time, nothing happens. For example, you don't see the trace statement in the Output panel. To see the trace statement, you have to call the helloWorld() function.
helloWorld();
This code calls the helloWorld() function.
The following text is displayed in the Output panel: Hello world!
For information on passing values (parameters) to a function, see Passing parameters to a function.
There are several different ways that you can write functions on the main timeline. Most notably, you can use named functions and anonymous functions. For example, you can use the following syntax when you create functions:
function myCircle(radius:Number):Number {
return (Math.PI * radius * radius);
}
trace(myCircle(5));
Anonymous functions are often more difficult to read. Compare the following code to the preceding code.
var myCircle:Function = function(radius:Number):Number {
// function block here
return (Math.PI * radius * radius);
};
trace(myCircle(5));
You can also place functions in class files when you use ActionScript 2.0, as the following example shows:
class Circle {
public function area(radius:Number):Number {
return (Math.PI * Math.pow(radius, 2));
}
public function perimeter(radius:Number):Number {
return (2 * Math.PI * radius);
}
public function diameter(radius:Number):Number {
return (radius * 2);
}
}
For more information on writing functions in a class file, see Classes.
As you can see in the previous code sample, you don't need to place functions on a timeline. The following example also puts functions in a class file. This is a good practice to adopt when you create large applications by using ActionScript 2.0, because it lets you reuse your code easily in several applications. When you want to reuse the functions in other applications, you can import the existing class rather than rewrite the code from scratch or duplicate the functions in the new application.
class Utils {
public static function randomRange(min:Number, max:Number):Number {
if (min > max) {
var temp:Number = min;
min = max;
max = temp;
}
return (Math.floor(Math.random() * (max - min + 1)) + min);
}
public static function arrayMin(num_array:Array):Number {
if (num_array.length == 0) {
return Number.NaN;
}
num_array.sort(Array.NUMERIC | Array.DESCENDING);
var min:Number = Number(num_array.pop());
return min;
}
public static function arrayMax(num_array:Array):Number {
if (num_array.length == 0) {
return undefined;
}
num_array.sort(Array.NUMERIC);
var max:Number = Number(num_array.pop());
return max;
}
}
var randomMonth:Number = Utils.randomRange(0, 11);
var min:Number = Utils.arrayMin([3, 3, 5, 34, 2, 1, 1, -3]);
var max:Number = Utils.arrayMax([3, 3, 5, 34, 2, 1, 1, -3]);
trace("month: " + randomMonth);
trace("min: " + min); // -3
trace("max: " + max); // 34
month: 7 min: -3 max: 34
|
NOTE |
|
For information on writing code using Script Assist, see Using Flash. |
Flash CS3