Hello!
This is our fourth appointment with the basics of Actionscript 3.0 .
After having seen the variables, the loops and the Arrays, this time I will introduce you to the functions.
The functions can be extremely complex and this is not the final point of this tutorial.
The goal of this tutorial is to give a general explanation to those of you starting with Actionscript so to learn the basics and have a good base to increase your future knowledge in using the functions.
The functions are a set of instructions which is performed once called.
They are a portion of stand alone code, meaning that they can carry out the code without the help of other components.
The functions are composed of:
- an identifier
- some parameters
- some returned parameters
They can be called from various points of our application using the identifier and the parenthesis operator().
Simple function
Let us see how to define a simple function
Code:
function flep():void
{
trace('ok');
}
function ? I declare to Flash that it is a function
flep() ? is the identifier ( so to be able to call it back later on. More or less like the name of the function) and the parenthesis () which for the time being do not contain any parameters
:void ? let Flash knows that this function does not return any value (the :void is not obligatory but it is a good habit to take to make you remember that a function could return a value)
the code in between the brackets is the code witch will be performed by Flash when the function is called. If I publish the SWF at this time, nothing will happen as the function as not been called into action.
How to call the function flep?
This way:
Code:
function flep():void
{
trace('ok');
}
flep();
using the identifier and the operator parenthesis ().
Inside the function, there is a command which will be carried out by Flash. If I publish the SWF at this time, I will obtain the following output:
ok
Let us next make a concrete example using a MovieClip.
I create a MovieClip (the shape is not important) and having placed an instance of it on stage, I give it the instance name ?clip_mc?
I position it in the top left angle of the stage.
Now, I want to move clip_mc to some chosen coordinates x and y on stage.
To do so, I create a function named moveClip the following way:
Code:
function moveClip():void
{
clip_mc.x=300;
clip_mc.y=250;
}
and I call it back this way:
If I publish the SWF at this time, I notice that the MovieClip has moved to the desired coordinates as declared in between the brackets of the function moveClip.
Another example of a simple function:
Code:
var flower:String;
function assignName():void
{
flower='daisy';
}
assignName();
trace(flower);
I created a string type variable named flower.
I created a function named assignName and in between its brackets, I assign a value to the variable flower.
If I publish the SWF at this time, obviously, the variable flower will not have a value as the function assignName (containing the value of the variable flower) has not been called.
So, I call assignName() and immediately afterwards I tell Flash (via the command trace) to display the value of the variable flower. I obtain the following output:
diasy
Function that receives parameters
As said before, an Actionscript function can receive parameters.
This is a fundamental point as the function itself could perform the code included into it based on the value of the parameters sent to it.
Let us take a look at the first example:
Code:
var section:int=10;
function multiply(n:int):void
{
section=section*n;
}
multiply(5);
trace(section);
I created a variable named sezione of int type and I assigned to it the value of 10.
I created a function named multiply and in between its parenthesis, I assigned a parameter named n of type int.
The function will multiplied the value of the variable sezione by the value passed as parameter.
So, declaring sezione=sezione*n; means that the new value of sezione is the value of sezione multiplied by the value of n.
I call the function passing to it a parameter (a full number as the type int as been declared or Flash would return an error). In this case, I pass the number 5.
I ask Flash to show me the new value of sezione (via trace) and I obtain the following output:
50
I will now make an example using a MovieClip placed on stage (clip_mc).
I create a function that will move the MovieClip to the coordinates passed as parameters.
Code:
function moving(cx:Number,cy:Number):void
{
clip_mc.x=cx;
clip_mc.y=cy;
}
moving(200,200);
In this case, I implemented 2 parameters in reception to the function moving and that I assign respectively to the x and y of clip_mc.
If I publish the SWF at this time, I notice that clip_mc has moved to the declared coordinates.
Function that returns a value
An Actionscript function can also return a value.
Up till now, I used :void to tell Flash that the function does not return a value.
If I would change the :void to (as example) :Number, it would mean that this function will return a value of number type.
Let us look at it.
Code:
var people:Number;
function double(n:Number):Number
{
return n*2;
}
people=double(23);
trace(people);
I declare a variable named people of Number type.
I create the function double that wants a parameter of Number type and will return a parameter of Number type.
With the operator return, I tell Flash to return the value of n (the value received by the function) multiplied by 2.
I directly assign to the variable people the value returned by the function double:
people=double(23);
If I would publish the SWF at this time, I obtain the following output:
46
In brief, saying double(23); is the same as saying 46.
In fact if I do a trace(double(23));, the output would be 46.
Let us make another example with a MovieClip.
I create a function that each time it will be called, it will return a rounded shape MovieClip.
Code:
function createClip(radius:Number):MovieClip
{
var clip:MovieClip=new MovieClip();
clip.name='clip';
clip.graphics.beginFill(0xFF0033,1);
clip.graphics.drawCircle(300,300,radius);
clip.graphics.endFill();
return clip;
}
addChild(createClip(50));
trace(createClip(50).name);
I create a function named createClip that wants as parameter of Number type and returns a MovieClip.
Inside the function, I create a variable named clip of MovieClip type and I instantiate it, meaning I create a new MovieClip using the operator new. I assign a name to the MovieClip. I tell clip to draw a circle with the coordinates x 300 and y 300 and a radius based on the value of the parameter radius.
I return clip.
In this case, we can understand easily that the function returns a MovieClip as I add it to the stage using the method addChild the following way
addChild(createClip(50);
In fact, if I do a trace(createClip(50)); I obtain the following output:
[object MovieClip]
clip
Source files:
Bookmarks