Getting Started - Functions PDF Print E-mail
Written by Smash   
Friday, 17 July 2009 18:19

What are functions and how do i use them?

I suggest viewing the video at the bottom of this page first for a better understanding of the text.

Functions are sections of reusable code. If there is a certain task you do a lot, why not create a function for it to make the task simple and short? I remember when I started programming functions seemed daunting and not worth using because they seemed too complicated. As with all things in programming, the best way to face a problem is to dive in and try it, if it does not work, thats ok, keep trying, but if it does work it will boost your confidence and make you a better programmer!

To create a function you need a few basic things, a name, input types(optional), a return type(optional), and of course the code that goes inside. Lets look at a function

function myFunction():void
{
//your code here
}

This is a complete function, although it does not do anything yet. We create it with the word Function, we name it myFunction, then in the parenthasis we give it an input type(although we did not do that here), we give it a return type (here we use void because we dont need to return data) and finally we put out code in opening and closing curly braces { }.

To make our function do something lets practice by making our function run a trace statement with data we give it.

function myFunction(myData:String):void
{
 trace(myData);
}
myFunction("This is being run through myFunction!");

That wasent so hard, now we have a fully functional function. We are now using input data, myData:String to be exact. Then inside the code we are placing that data into a trace statement. Finally we run out function.

You can even manipulate Movie Clips with functions by using a Movie Clip as an input type like this

function myFunction(myMovieClip:MovieClip):void
{
 myMovieClip.x += 5;
myMovieClip.y += 5;
}
myFunction(myCircle);

With this function lets imagine I have a Movie Clip with the instance name of myCircle on the stage. This will take any movieclip I send it and raise its x and y position by 5. So when I run it with myFunction(myCircle); myCircle is being sent through the function and having its x and y position raised by 5. With this code you can edit any aspect of a movieclips properties which makes it a powerful function. We can even go a step further and give it another input type to determine how much we want to raise the x and y position.

function myFunction(myMovieClip:MovieClip, myNum:Number):void
{
myMovieClip.x += myNum;
myMovieClip.y += myNum;
}
myFunction(myCircle, 100);

Here is the same function, except with another input type, we can use any number instead of being stuck with 5. Here we raise the x and y property of myCircle by 100.

You can get the finished fla example HERE

You can make your functions as complex or simple as you want, they can include multiple input types and pretty much do anything. All the commands we use in ActionScript 3.0 themselves are functions!

 

 
Home > Tutorials > ActionScript 3.0 > Getting Started - Functions