in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

macromedia flash mx - Reinhardt R.

Reinhardt R., Lott J macromedia flash mx - John Wiley & Sons, 2004. - 987 p.
ISBN 0-7645-4354-7
Download (direct link): macromediaflash2004.pdf
Previous << 1 .. 67 68 69 70 71 72 < 73 > 74 75 76 77 78 79 .. 419 >> Next

No copy of the object is made. Here is an example using a MovieClip instance named box:
function move(mcA:MovieClip, x:Number, y:Number):Void{ mcA._x = x; mcA._y = y;
Working with the arguments Property
All the functions you’ve looked at thus far either do not use any parameters, or the parameters are declared as a parameters list within the parentheses. However, regardless of whether or not a function declares any parameters, all parameters passed to the function are stored in a special array named arguments. Each function has an arguments variable (object) that gets created within it when the function is called.
ActionScript does not enforce symmetry between the number of arguments in the function definition and the number of arguments passed to the function when it is called. That means that any values not passed in the call, but defined in the argument string for the function, have an undefined value. And any values passed in the function call that are in addition to the arguments defined for the function are discarded.
130 Part II ¦ Laying the ActionScript Foundation
Using the arguments Object to Reference Other Functions
Every arguments object has two special properties that reference functions. These properties, caller and callee, although not often used, can be useful in some circumstances — especially when developing highly abstract functions.
The caller property of the arguments object returns a reference to another function, if any, that called the current function. If the current function was not called from another function, the caller property has a null value.
function function1():Void{ function2();
function function2():Void{
if(arguments.caller == function1)
trace("function2 called from function1");
trace("function2 not called from functionl");
In this example, the Output window displays the following:
function2 called from function1 function2 not called from function1
The callee property of a function's arguments object is a reference to the function itself. It may not be apparent immediately why this is useful. But consider the scenario of an anonymous (see the section "Creating Function Literals") recursive function (see the section "Creating Recursion") for a moment. You can write a function literal that is capable of calling itself recursively as follows:
factorial = function(n:Number):Number{ i f(n > 0){
return n * arguments.callee(n-1);
return 1;
Therefore, it is entirely possible to define no parameters in the function declaration but rather rely on using the arguments object. Here is an example of how you can use the arguments object as an array:
function traceParams():Void{
for(i = 0; i < arguments.length; i++){ trace(arguments[i]);
traceParams("one", "two", "three");
Chapter 6 ¦ Working with Functions
In this example, the following is displayed in the Output window:
In the majority of functions, it is far better to declare the parameters. The arguments object is mostly useful when you are overloading a function (see the section “Overloading a Function”) or other similar situation. The arguments object is mentioned here for completeness and also as a reference for when it is mentioned later in this chapter and in other parts of the book.
Overloading a Function
Overloading a function normally involves having multiple functions with the same name but with different numbers of parameters. This can be useful in a lot of situations. For example, you could have a function named calculateArea() that calculates the area of a rectangle based on two parameters (the lengths of the sides). But you might also want to have a calculateArea() function that calculates the area of a circle based on a single parameter (the radius). The trouble is that, as already mentioned, ActionScript does not require symmetry between the number of parameters defined for a function and the number of parameters passed to it. That means that you cannot have two functions with the same name — even if they expect different numbers of parameters.
You can simulate function overloading if you desire by using if statements in the function to check for the number of parameters. The following shows how you can write a function that calculates the area of either a rectangle or a circle depending on the number of parameters it is passed (determined by arguments.length):
function calculateArea():Number { switch(arguments.length) { case 1:
var radius:Number = arguments[0]; return (Math.PI * (radius * radius));
case 2:
var a:Number = arguments[0]; var b:Number = arguments[1]; return (a * b); default:
return null;
Returning a Value from a Function
Up to this point, you’ve mainly looked at functions that serve as subroutines. That is, the functions can serve to break up the main routine into smaller, more manageable chunks. In the cases where a function operates as a subroutine in that fashion, the function does not need to return a value. On the other hand, sometimes you want to create a function that performs some calculations or operations and then returns a value.
Previous << 1 .. 67 68 69 70 71 72 < 73 > 74 75 76 77 78 79 .. 419 >> Next