Local and Global Variables in AS3
Tutorials

Local and Global Variables in AS3


In the example below, you will see two variable declarations and one function:

var outsideVar:String = "outside of the function";

function myFunction():void
{
var insideVar:String = "inside of the function";
}

myFunction();

In the example above, we have a variable named outsideVar that's declared outside of the function, and another one named insideVar that's declared inside of the function.

Variables that are declared inside functions are referred to as local variables. So in this example, insideVar is a local variable. It is important to make this distinction because a local variable can be accessed only within the function that it was declared in.

Try to trace the local variable using a trace statement, but place this trace statement OUTSIDE of the function:

var outsideVar:String = "outside of the function";

function myFunction():void
{
var insideVar:String = "inside of the function";
}

myFunction();

trace(insideVar);

Test the movie, and you will see that this will result in an error message that says: Access of undefined property. The undefined property being referred to in this case is the local variable that you are trying to access. It's being seen as undefined because the trace statement can't find the variable. This is because the variable is inside the function and is therefore local, while the statement trying to access it is outside of the function. Trying to access a local variable from outside the function will not work. Think of it this way: it's like the function is hiding the local variable from all the outsiders. Only the ones inside the function can see it. Everyone else can't.

So if we put the trace statement inside the function instead, we'll be able to trace the local variable when the function is called (remember to call the function, otherwise it won't run).

var outsideVar:String = "outside of the function";

function myFunction():void
{
var insideVar:String = "inside of the function";
trace(insideVar);
}

myFunction();

So now, if you test the movie, you'll be able to see the value of the local variable displayed in the output window.

On the other hand, a variable that's declared outside of a function can be accessed from inside a function as well as outside. For example, if we try to trace outsideVar using a trace statement inside the function, this will not give us any errors:

var outsideVar:String = "outside of the function";

function myFunction():void
{
var insideVar:String = "inside of the function";
trace(insideVar);
trace(outsideVar);
}

myFunction();

If we try to trace outsideVar from outside of the function, we won't get any errors as well:

var outsideVar:String = "outside of the function";

function myFunction():void
{
var insideVar:String = "inside of the function";
trace(insideVar);
}

myFunction();

trace(outsideVar);

The variable named outsideVar, which we declared outside of the function, is referred to as a global variable - it is a variable that can be accessed in all areas of our code. We can access it from inside functions or outside of them.

Deciding between making a variable global or local
If you need the variable to be accessed in all areas of your code, then you can make it global by declaring it outside of any function. If the variable is only going to be used within a function's body and nowhere else, then make it local by declaring it inside the function that's going to use it.




- As3 Countdown Timer - Creating A Simple Countdown Timer In Flash Using Actionscript 3
In this tutorial, we'll learn how to create a very simple AS3 countdown timer in Flash. Step 1 Create a new Flash ActionScript 3 document. Select the text tool once your document opens up. Step 2 Go to the Properties Inspector and choose the following...

- Part 1 - Creating Actionscript 3 Variables And Assigning Values To Them - Introduction To Flash As3 Variables
PART 1: Creating ActionScript 3 Variables and Assigning Values to themPART 2: Variable Naming Rules and ConventionsPART 3: Fun with Variables - Complete the Story Welcome to this tutorial series on an Introduction to Flash AS3 Variables. In part 1 of...

- The As3 Event Object
Part 1: Introduction to AS3 event handling Part 2: How to create an AS3 event listener Part 3: The AS3 event object by Alberto Medalla Lecturer, Ateneo de Manila University When an ActionScript 3 event occurs, an event object gets created just before...

- As3: Enabling The User To Submit The Contents Inside An Input Text Field
In this lesson, we're going to learn how to add some ActionScript 3 functionality that will let a user submit the contents of an input text field by clicking on a button or pressing a keyboard key. We'll create a simple example of a Flash movie...

- How To Pass Additional Parameters To As3 Event Handler Functions
I've recently made the leap to AS3, and one of the very first questions I wondered about was how I could pass extra arguments to event listener functions. From what I understand, event listener functions in AS3 can only have one parameter, which is...



Tutorials








.