About functions and methods
219
After passing the parameter, you can pass a value to the function when you call the function.
This value traces in the Output panel, as follows:
myFunc("This is what traces");
When you call the function, you should always pass the specified number of parameters unless
your function checks for undefined values and sets default values accordingly. The function
substitutes the passed values for the parameters in the function definition; if any parameters
are missing, Flash sets their value to
undefined
. You regularly pass parameters into functions
when you write ActionScript code.
You can also pass multiple parameters to a function, which can be as simple as the following:
var birthday:Date = new Date(1901, 2, 3);
trace(birthday);
Each parameter is separated by a comma delimiter. Many built-in functions in the
ActionScript language have multiple parameters. For example, the
startDrag()
method of
the MovieClip class takes five parameters,
lockCenter
,
left
,
top
,
right
, and
bottom
:
startDrag(lockCenter:Boolean, left:Number, top:Number, right:Number,
bottom:Number):Void
To pass a parameter to a function:
1.
Create a new Flash document and save it as
parameters.fla
.
2.
Add the following code to Frame 1 of the Timeline:
function traceMe(yourMessage:String):Void {
trace(yourMessage);
}
traceMe("How are you doing?");
The first few lines of code create a user-defined function called
traceMe()
, which takes a
single parameter,
yourMessage
. The last line of code calls the
traceMe()
function and
passes the string value
“How are you doing?”
.
3.
Select Control > Test Movie to test the Flash document.
The next example demonstrates how to pass multiple parameters to a function.
To pass multiple parameters to a function:
1.
Create a new Flash document and save it as
functionTest.fla
.
2.
Add the following code to Frame 1 of the main Timeline:
function getArea(width:Number, height:Number):Number {
return width * height;
}
The
getArea()
function takes two parameters,
width
and
height
.
Summary of Contents for FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Page 1: ...Learning ActionScript 2 0 in Flash...
Page 8: ...8 Contents...
Page 18: ...18 Introduction...
Page 30: ...30 What s New in Flash 8 ActionScript...
Page 66: ...66 Writing and Editing ActionScript 2 0...
Page 328: ...328 Interfaces...
Page 350: ...350 Handling Events...
Page 590: ...590 Creating Interaction with ActionScript...
Page 710: ...710 Understanding Security...
Page 730: ...730 Debugging Applications...
Page 780: ...780 Deprecated Flash 4 operators...
Page 830: ...830 Index...