Using Functions in ActionScript 3
Using Functions in ActionScript 3
0
By Blue_Chi | Flash CS3 | ActionScript 3.0 | Beginner
Functions are fundamental tools used in ActionScript to reduce the amount of written code, save time, and add
a logical structure to your code. This tutorial will describe the basic usage of functions in ActionScript 3.0.
This is an ActionScript 3.0 tutorial. To learn about using Functions in AS2 please review our tutorial on that
topic.
Our tutorial is divided into these sections:
What is a Function
Function Code Example
Basic Function Usage
Advanced Function Features
Functions and Variables
What is a function?
A function is a block of code wrapped together in a way so that it can be re-used at anytime in the code. It is
used to save time as repeated tasks can be written only once and then called multiple times. It centralizes the
code so that all of it is placed in a specific place that can be easily updated without having to manually modify
each time the code is used. Using functions also makes reading the code by humans easier as it could be
used to logically arrange code performing a collective task in a single place.
Function Code Example
For example, say that you want to create an instance of a certain Star object, set its x and y properties, and
add it to the display list. You can do that by using the code below:
var myStar:Star;
function makeStar():void {
myStar = new Star();
myStar.x = 100;
myStar.y = 150;
addChild(myStar);
}
makeStar();
makeStar();
The code above will create two stars because the function has been called twice. The section below will
explain how this works in detail.
Function Basics
Any function could be written using the generalized code shown below:
function functionName(argument):returnType{
statements;
}
The first requirement for creating a function is to use the function keyword. You simply follow this by the name
you want your function to have. You can almost use any name for this. We are going to use the
name magicalFunction :
function magicalFunction(){
}
The code which the function will execute must be written between the curly brackets {} . We are going to make
this function create a new MovieClip and set its .x and .y properties:
function magicalFunction(){
var myMovie:MovieClip = new MovieClip();
myMovie.x=100;
myMovie.y=150;
}
The process for creating a function is called defining a function, in order to use a function you need
to invoke it. Invoking the function is a separate process that requires you to type the function name followed
by two brackets:
function magicalFunction(){
var myMovie:MovieClip = new MovieClip();
myMovie.x=100;
myMovie.y=150;
}
magicalFunction();
That last command will execute your function, you can simply type magicalFunction anytime you wish to
create new MovieClip and set its x and y properties to 100 and 150 respectively. You must always remember
to invoke your function because simply defining it will not execute any code.
Advanced Function Features
You might have noticed that our earlier function was a generic code that contained a number of parameters
which we did not use:
function functionName(argument):returnType{
statements;
}
The first of these is the argument parameter, using this feature lets you create flexible functions which can be
reused to carry out a unique action each time the function is invoked. An argument can hold a reference to an
object or variable which will be passed when the function is invoked so that it can be used with different objects
or variables.
To make things clearer, we might want to create Star objects which are created at a different position each
time a new one is created, our previous code would have created stars that were positioned directly over each
other:
var myStar:Star;
function makeStar():void {
myStar = new Star();
myStar.x = 100;
myStar.y = 150;
addChild(myStar);
}
makeStar();
makeStar();
We can pass an argument to the function so that we specify the x and y values when the function is called by
specifying these values between the brackets:
var myStar:Star;
makeStar();
makeStar();
You should notice that our function does not have any actual value for the myX and myY variables, these can
be specified when the function is invoked, and not only that, but you can specify different values each time you
invoke the function:
var myStar:Star;
makeStar(100,200);
makeStar(150,320);
Using the code above, the first star will be placed at 100, 200 while the second one will be placed at 150, 320.
This shows the power and flexibility of functions, it can be further modified so that it creates a different shape
or a different star size depending on what values the user specifies when the function is invoked.
In addition to this, a function can be used to perform a process and then return a result. This is an advanced
technique, a very basic example of it is as follows:
function calculate(){
var num1:Number = 50;
var num2:Number = 100;
var myResult:Number;
myResult = num1+num2;
return myResult;
}
This value can then be used in various situations for example, you can now set this function as the x property
of an object you wish to position at that number. You know that num1+num2 will equal 150, so the object will
be positioned at 150.
function calculate(){
var num1:Number = 50;
var num2:Number = 100;
var myResult:Number;
myResult = num1+num2;
return myResult;
}
myResult = num1+num2;
return myResult;
}
makeStar(100,200);
makeStar(150,320);
Specifying the data type as void when no return value is expected is good AS3 practice, make sure you do it
all the time.
Final Comment About Function Variables
You should be aware of the effect of creating a variable inside a function. Using the var keyword inside a
function will lead to the creation of a local variable that will not be accessible outside the function. For
example, in the code below, the variable myResult will not be accessible from outside the function:
function calculate ():Number {
}
If you try to access the myResult variable outside the function, you will receive an error message:
function calculate ():Number {
trace(myResult); // THIS WILL GENERATE AN ERROR - MYRESULT DOES NOT EXIST OUTSIDE
THE FUNCTION.
If you would like to have your variable accessible outside you function you will have to define it outside the
function and then simply modify it from inside the function:
var myResult:Number;