Local and Global Variables in ActionScript

by Milan Midovich.

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

You are here: Categories » Computers and technology » Flash

Using variables in ActionScript is easy. All you need to do is assign a value to a variable name. Here is an example:

myVariable = 7;

The preceding line creates the variable named myVariable and places the number 7 inside it. Note that the name myVariable was chosen arbitrarily by me. You could name the variable anything. For instance, numberContainer, a, or fred would all work.

To see variables in action, you can test them with the Output window. Here is a short program that you can place in the first frame of a blank movie:

myVariable = 7;
   trace(myVariable);

When you run this movie, the Output window appears with the number 7 in it. The number 7 was stored in myVariable and then the trace command was used to place the contents of myVariable in the Output window.

Global Variables

A global variable is one that is accessible throughout the entire level of the Flash movie. You can set it in one frame, and it will still contain its contents in another frame.

You don't need to do anything special to create a global variable. Just using it, like in the previous example, automatically makes the variable a global one.

In most programming languages, global variables are available everywhere. However, Flash movies use a system of levels. The main movie timeline is the root level. Any movie clips are actually small Flash movies inside the main one. The graphics and scripts inside a movie clip are one level down from the root level. Global variables at the root level aren't accessible inside a movie clip—at least not directly.

Local Variables

Local variables, unlike globals, are only available in the current script. In the next frame, the variable won't exist. You can certainly create a new variable with the same name, but the previous contents from the last frame will not be in it.

The point of local variables is to create modular code. If a variable is local, it is removed from memory when the script is finished. Otherwise, if it is a global variable, the variable and its value will hang around until the movie ends.

To create a local variable, you need to use the var keyword. For instance, you could create a local variable named myLocal and place the number 9 in it like this:

var myLocal = 9;

After you set the variable with the var keyword, you don't have to use var again in that local piece of code. For instance, the following code creates the local variable, sets it to 9, changes its value to 11, and then sends it to the Output window:

var myLocal = 9;
   myLocal = 11;
   trace(myLocal);

When deciding when to use local variables and when to use global variables, the rule of thumb is to always use local variables unless there is a good reason to use a global. We'll mostly use local variables.

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
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...)
3D Scaling with ActionScript - Although Flash is not capable of real 3D graphics, the kind seen in popular computer games, you can create the illusion of 3D by using scaling. Scaling an object is a great way to give your (more...)
The Dot Syntax - Something else that you will be seeing a lot of as you learn ActionScript is dot syntax. Dot syntax is a way of grouping objects and functions that is used in many object-oriented programming (more...)
Movie Clip Scripts - Movie clip scripts, like button scripts, use handlers. Instead of using the on keyword that button scripts use, we will use the onClipEvent keyword. Here is an example of a movie clip script: (more...)
Decoding an RGB Value - You want to extract the red, green, and blue components from an RGB value returned by Color.getRGB( ). Use the bitshift right and bitwise AND operators. You can extract the red, gre (more...)
ActionScript: Generalizing a Function to Enhance Reusability - You want to perform slight variations of an action without having to duplicate multiple lines of code to accommodate the minor differences. Add parameters to your function to make it flexib (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.