Home | Website Design Samples | Art & Design | Multimedia | Services | Contact | E-mail Me
Flash Chat | Flash Test | JavaLab | Pic Resizer | Articles | Snip A Site | Link Lister
| Pay James
Find out what's happening with the Web locally—Check out my Meetup at the Brampton Web Design Meetup Group.
I have worked with companies in the U.S.A, Canada and Europe and can help you with your Web development and design on an hourly or project basis. Contact me for an estimate or give me a call.

FlashMX Article: Variables

Introduction
To keep track of values and expressions, you need to use variables. You can think of a variable as a bucket that you can pour “stuff” into. And just like a bucket, you can empty it, you can add more to it, and you can also change the contents. You can use a variable to keep track of, say, the number of times a person clicks a button in a Flash movie. Another use of a variable is to keep score. You will most likely always use variables when you code in Actionscript.

How to Use a Variable
Before you can use a variable, you need to initialize it. All this means is that you need to “pour” or “stuff” a value in it. Going back to our bucket example, it wouldn’t make sense to just pour water into nothing, would it? You first need a bucket. You need something to act as the container for your water, right? Now stay with me, so in like manner, in order to use a variable, you have to initialize it. Like this:

MyVariable=150;

Notice that the value is on the right side of the variable “MyVariable”. To use our bucket example as an analogy, it’s tantamount to saying, “Okay, James, see that bucket labeled MyVariable? Well, take that number 150 and pour it into or put it into the bucket labeled MyVariable.”

The Scope of a Variable
When I first heard of scope, it sounded really technical. It isn’t. Think of scope as what or who can see or use the variable. It helps to think of the word telescope. You know, you’ve must’ve seen movies where the old crusty sea captain looks through his “scope” and sees Moby Dick or something, and then when the camera switches to what the captain sees all you see is a black background and a circle in the middle of the screen to show you what the captain sees. Well, to labour this analogy further, what you see can be called the scope. In much the same way, when variables are created, they can only be seen based on their scope.

Where you create a variable is where its scope is said to be. For example, if you created a variable in the main timeline, the variable can be seen or used by other code in the same timeline. However, if you created a movie clip (which has its own timeline), and initialized a variable there, then the scope is within the movie clip only. In other words, code in the main timeline will not be able to see, access, take note of, use the variable in the movie clip. Technically, you can have two variables with the same name as long as the scope is different. However that is not good programming habits.

_global
Okay, but what happens if you want to be able to access a variable from both the main timeline and the movie clip? You will need to use the _global identifier. Just as it sounds, it will make the variable available and accessible from any part of FlashMX. Any time, any where. Here’s an example on how to use the _global identifier:

_global.MyVariable=150;

That’s it. By preceeding the “MyVariable” variable with _global, it’s makes it available from any part of FlashMX.

Conclusion
Variables are essential in every programming language. Actionscript is a simple and lean language to pick up to breath some life into your FlashMX projects. When working with variables, just think of them as a container or bucket that you use to store values and when using them in expressions.

Comments?

Contact me now with my online e-mail application:

Mailing Address: 12 Paradise Gardens, Brampton, ON L6S 5C7 Phone: (905) 793-2690  Fax: (905) 790-7412
© Copyright 2003 James Tadeo All trademarks, copyrights, and branding belong to their respective owners.