Jul 06 2008
Variables
What is a variable? It is essentially a representation of a number, string of characters, or anything else that can change values.
Remember math class? Remember how “x” never really was the letter x, instead a number could be stored into x and x could change? Well, that is how variables work in ActionScript.
Types:
Flash has tons of different variables. Some of them are a lot more complex than others. In this entry I am just focusing on the simpler variable types.
Primitive variables: Think of them as your nuts and bolts. You can’t do much with them except a few basic gestures and they are designed for a specific application.
- int - Stores a positive or negative whole (…-3, -2, -1, 0, 1, 2, 3…) number
- uint - Stores a positive whole number ( 1, 2, 3…)
- Number - Stores any number value including decimals ( 1.23, 3.14, -2.25)
- String - Stores a set of characters. When numbers are included, they are considered characters and you cannot perform mathematical operations on them. ( Bob, AndyGCook, Catch22)
- Boolean - Stores a true/false value.
Declaring:
In ActionScript 3.0, you have to declare all your variables before you can create them. Declaring a variable means actually creating it in your program. The syntax to create a variable is:
var variableName:Type;
All variables start with the word var. This is no optional!
The next thing is the variable name. Variables can start with a letter ( a, b, c ) or an underscore. That’s it! After that, you can only have letters, numbers, and underscores.
Third part is a colon, then after the colon comes the variable type. Just like the ones I mentioned earlier ( Number, Boolean, String, etc).
Defining
Once you declare your new variable you can start using it. When you set a value to your new variable, that is called defining. To store a value:
var nHeight:Number;
nHeight = 75;
When you create your variable, that is the only time you use the var keyword. Other than that, you can just call the variable whenever you want.
More samples of declaring and defining variables:
var bBrunette:Boolean = true; var sName:String = "AndyGCook"; /* When you declare a string, you have to put it in " ". This helps Flash recognize that you want your variables to use characters instead of numbers, etc. */ var nHeight:Number = 75.25; var intAge:uint = 85; var uintPages:uint = 300; /* I used a uint for pages because you can never have an odd number of pages and there can never be negative pages.*/
Conventions:
A convention is a generally accepted principle within the programming community. Conventions are not laws, but rather ideas everyone agrees upon to make sharing code easier.
Imagine working on a project and being used to your own style of programming. All of the sudden, your teammate gives you her code to work on, and it is in a completely different style that is foreign and hard for you to read. This is why we use conventions. To make everyone’s life easier.
Be descriptive: Be descriptive when you name your variables. This will help you identify what the variable is actually used for.
Use Camel Case or underscores: Camel case means you start your first word with a lowercase letter, and then the rest of the new words start with a capital letter. Using underscores means putting underscores between words.
Examples:
myVariableName bestFriendsName shortestBasketballPlayer
my_variable_name best_friends_name shortest_basketball_player
Be consistent: Make sure you stick with your style once you find one that is right for you.
My own personal preference: If you noticed above, I start all my variables with identifiers. They act as quick ways to help me identify variable types.
String: s
Boolean: b
Number: n
int: int
uint: uint
Why do I do this?:
Naming variables like this will easily help you identify what a variable is without looking for where you declared it. If you have a variable called cat, at first glance that could be almost anything. If it is called sCat, you know right away a string goes into the cat variable.
Those are the basics of variables. I hope this post helped the beginners out there.