Functions in ActionScript

by Milan Midovich.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on flash  

You are here: Categories » Computers and technology » Flash

Functions allow you to organize and reuse your code. You place functions in the timeline just as we have been doing. Here is a simple function:

function myFunction(num) {
   var newNum = num + 3;
   return newNum;
   }

A function starts with the keyword function followed by the function name. Function names can be anything you want, just like variable names. But they should usually be something that relates to what the function does.

After the function name comes a left parenthesis. Then follows a list of parameters. A parameter is a variable that is defined when the function is called. Think of it as the input to a function. In this case, you are going to give the function a number to do something with.

You can have one, many, or no parameters. Either way, you close off the parameters section with a right parenthesis and then use an open bracket to start the function.

All the lines between the open and close brackets are the instructions inside the function. In this case, a new local variable is created, called newNum. The value of newNum is set to whatever num is, plus 3. So if you pass a 7 in to the function as num, newNum is now 10.

The return command is a special command used only inside functions. It completes the function and sets a value as the result of the function. In this case, newNum is the result of the function.

To use this function, call it like it was a standard ActionScript function or command, such as trace. Here is an example:

var a = myFunction(7);

This line of code creates a new local variable called a. It places in it the results of myFunction(7). To determine this value, myFunction is called with the number 7 as its only parameter.

When the function starts, it creates a local variable called num and places 7 inside it. It then runs the code inside, which ends with the return command sending the value 10 back to the thing that originally called the function. In this case, a gets set to 10.

A great thing about functions is that you can reuse them. Here are three lines of code that reuse the function to produce three different results:

trace(myFunction(7));
   trace(myFunction(13));
   trace(myFunction(2));

When you run this code, along with the function included before it, you will get the results 10, 16, and 5. Another advantage to using functions is that you can make one change in the function, and it will affect all the commands that use that function. For instance, if you change the + 3 in the function to + 4, the results of the preceding three lines become 11, 17, and 6.

Leave a comment or ask a question
Total comments: 0

Flash Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
Avoiding Conflicting Variables in ActionScript - You want to make sure that variables within a function do not interfere with variables in other functions or within the timeline in which the function is defined. Use the var keyword to declare loc (more...)
How to Exit a Function in ActionScript - You want to exit a function. Functions terminate automatically after the last statement within the function executes. Use a return statement to exit a function before reaching its end. The (more...)
Move Clip Rotation - A property like _x and _y is the movie clip property _rotation. The _rotation property accepts a value in degrees. A circle is divided into 360 degrees. The values used by _rotation range f (more...)
Targeting Flash Movie Clips - The simplest way to target a movie clip is to use its name, followed by a dot, followed by the command you want to send. However, there are plenty of other ways to target a movie clip as we (more...)
Using Mathematical Operators in ActionScript - You want to modify something over time, such as the rotation or position of a movie clip. Use the compound assignment operators to change a variable or property in increments. Or, if increm (more...)
How to build an animated button using Flash - Time: 15 Minutes. Difficulty Level: Intermediate Requirements: Flash 8 Assumed Knowledge: Basic action (more...)
Enhancing Standalone Projector - You want to create an enhanced Standalone Projector with features such as borderless playback, custom titles, no Flash menus, and so on. Use a third-party tool such as SWF Studio or SWFKit to c (more...)
Creating Flash Buttons - Buttons are one of the three main types of symbols that you use in Flash. The others are movie clips and graphics. Making a New Button There are many ways to create a button. One (more...)
Bouncing Ball Script - Start a new Flash movie. Create a movie clip that has a ball graphic inside it. You can name the instance of the movie clip, myClip, but our code will not depend on the name of the clip. (more...)
ActionScript: Creating Reusable Code - You want to perform a series of actions at various times without duplicating code unnecessarily throughout your movie. Create a function and then call (i.e., invoke) it by name whenever you (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.