For whatever reason I feel like doing a mind dump of some ActionScript knowledge. I often struggle with what level “actionscripter” I should target, so I’ve decided on starting somewhere near the beginning. In this case I’ll discuss variables and what they’re used for. Maybe at some point I’ll put together a nice mini-book on ActionScript. Ah, the future…
What the heck is a variable?
Think of a variable as a container. A container that you can place different types of information in and recall at a future time. Whenever I try explaining a variable to newbs I always use the example of Nike iD. Are you familiar with the site? Nike iD is essentially a custom Nike shoe design shop. Anyone can go to the website, choose a stock shoe, and do all the customizing themselves. Fabric, leather, color, and shoelaces are on the table. Cool! But, from a developers perspective, this is going to require the use of variables.
When the user is done designing the shoe, the developer needs to record what colors and fabric options have been chosen. A good way to do this is with variables. The below animation serves as a good visual.
Note: the above code is not the actual code that would recreate this example. I am just using it to illustrate the changing of the variable shoeColor. You’ll notice that when a color is chosen that the hex value of the variable changes.
The theory here is that we are saving the color of the shoe that the user has chosen to a variable, or container, called shoeColor. This way when the order for the shoe needs to be shot off to accounting and order fulfillment, everyone knows what color the shoe is supposed to be. Hopefully that makes sense.
Declaring variables
Hopefully you now understand the basics of what a variable is. Now let’s spend some time looking at how they are actually declared. In AS3 there are a few rules you have to follow when creating a new variable. First, they are only alphanumeric, but will accept underscore (_) or the dollar sign ($). If you’re coming over from PHP you’ll probably be accustomed to using the dollar sign with variables. Secondly, AS3 variables can only be one word, and should not start with a number. Lastly, variables are case sensitive, so myVariable is different from MyVariable.
Another important thing to understand about variables is the idea of data types. As ActionScript has progressed as a language it has become more strict with the handling of data types. What is a data type you ask? A data type is a way of defining the type of variable you are working with. In our above example the data type was dealing with color. We can also deal with strings (or text), numbers, arrays, etc. Here is a quick table for your reference:
| Data Type | Example | Description |
|---|---|---|
| String | “my name is Matt” | A string of characters |
| Number | 7.3 | Any number, can include decimals |
| int | -3 | Any integer or whole number |
| uint | 2 | Unsigned integer or any non-negative whole number |
| Boolean | true | True of False |
| Array | [1, 3, 5] | A series of values in a single variable |
| Object | sampleObject | Essentially a custom form to store multipe values |
With the above table you are now ready to see the syntax for actually declaring the variable. Below are a few examples of variable declarations:
var myString:String = new String(); myString = "This is probably the best string ever written."; var myNumber:Number = new Number(); myNumber = 17; var myArray:Array = new Array(); myArray = [2, 5, 6, 7];
Hopefully this has clued you in to variables in ActionScript 3. You’ll want to get very comfortable with the concept of variables as you’ll use them in nearly everything you do. Feel free to ask any questions if I’ve left things hazy at all. I’m off to Home Depot to buy some spray paint and masking tape for a little art project on my ski helmet. Peace.

















