Functions in ActionScript

written by: Milan Midovich; article published: year 2007, month 07;



In: Categories » Computers and technology » Flash » Functions in ActionScript

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.

legal disclaimer

1) Our website is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. Please read the Terms of service

Useful tools and features

Translate this article to...    Send this article to you or to a friend

Link to this article from your page   
If you like this article (tutorial), please link to it from your web page using the information above. Linking to this page, this is the only way to help us improve our service, the same time providing your visitors with a way to improve their online experience.

related articles

1. ActionScript: Filling a Shape with a Solid or Translucent Color
Use the beginFill( ) and endFill( ) methods to initiate and close a shape drawn at runtime. To draw a filled shape, call beginFill( ) prior to any other drawing methods, including the custom methods you have defined such as drawCircle( ) and drawPolygon( ). Invoke endFill( ) after calling other drawing methods to create the shape. You cannot apply a fill to an existing shape drawn at authoring time or runtime. You must invoke beginFill( ) before drawing the shape to be filled. This example creates a solid blue ...

2. Drawing a rectangle using ActionScript
Create a custom MovieClip.drawSimpleRectangle( ) method using the Drawing API and invoke it on a movie clip. To draw a simple rectangle, specify the stroke's attributes using the lineStyle( ) method and then draw four lines using the lineTo( ) method: // Create rectangle_mc with a depth of 1 on the main timeline. _root.createEmptyMovieClip("rectangle_mc", 1); // Specify a one-pixel, solid, black line. rectangle_mc.lineStyle(1, 0x000000, 100); // Draw four lines to form the perimeter ...

3. How to draw a rectangle with rounded corners
You want to draw a rectangle with rounded corners, an offset, or rotation.Create a custom MovieClip.drawRectangle( ) method using the Drawing API and invoke it on a movie clip. The drawSimpleRectangle( ) method is, as the name suggests, quite simple. Let's create a more complex version that also: Draws a rectangle with a specified angle of rotation Let's you specify the rectangle center's coordinates Can draw a rectangle with rounded corners The drawRectangle( ) method accepts...

4. Reusing and Organizing Code in Flash Movies
You want to reuse code that you've created for one project in another Flash movie. Or you want to write your ActionScript code in an external text editor. Place your ActionScript code in external .as files and use the #include directive to add them to your Flash movies: // Adds all the code within MyActionScriptFile.as to your Flash movie. #include "MyActionScriptFile.as" Use the #include directive to incorporate code from external text files into your Flash movie during compilation from a .fla file ...

5. Drawing a Circle using ActionScript
Create a custom MovieClip.drawCircle( ) method using the Drawing API and invoke it on a movie clip. You can create a circle in ActionScript with eight curves. Fewer curves results in a distorted circle and too many curves hinders performance. Let's create a custom method of the MovieClip class for drawing circles. This method, drawCircle( ), allows for three parameters: radius The radius of the circle x The x coordinate of the circle's center point. If undefined, the circle is centered at x =...

6. ActionScript: Repeating a Task at Timed Intervals
You want to perform an action or actions at a specific timed interval. Use the setInterval( ) function. The setInterval( ) function allows you to specify an interval (in milliseconds) at which your Flash movie will invoke a function. Use setInterval( ) to perform a particular action over time but not necessarily at the frequency of the frame rate of the movie. // Define a function. function myIntervalFunction ( ) { // Output the difference between the current timer value and its value from the ...

7. Mouse Location Flash Script
Not only can you get the location of a movie clip on the screen, you can even get the location of the mouse, also known as the cursor. What is the difference between the mouse and the cursor? The mouse is the physical device attached to your computer. You may even have a track pad or tablet instead. The cursor is the graphic that moves around the screen as you move your mouse. So, technically, cursor is the term I should be using here. However, ActionScript uses the term mouse in its keywords. I will therefore use mouse and cursor i...