Understanding variables

show more Understanding variables provides you with in-depth training on Developer. Taught by Todd Perkins as part of the ActionScript 3.0 in Flash Professional CS5 Essential Training show less
please wait ...

Understanding variables

Variables are the building blocks of many a programming language, including ActionScript 3. Knowing what variables are and how to create and modify them is essential for building any application. I'm going to open up the Actions panel by selecting the first keyframe of layer 1 and pressing Option+F9 on the Mac, F9 on the PC, or by using the menu command Window > Actions. I'm going to create a variable by typing the word 'var.' That's var, short for variable, all lowercase.

If you typed it correctly, it should be blue. In Flash, reserved keywords will turn blue when you type them. That's all based on your ActionScript settings that you can set through Flash Preferences. After var, type a space and then type the name of the variable. So, just type 'my' and then Name with a capital N, no space. When you're naming a variable, you have to follow special rules.

Don't start with a number, but you can use numbers, as long as it doesn't start with a number. Then don't use any spaces or special characters, like an exclamation point or at symbol. After you type the name of the variable, you can type a colon to declare the variable datatype. Think of a variable like a noun. So a noun could be a person, place, thing, or idea. When I tell the datatype of a variable, it's the same thing as saying the type of noun.

It's like putting it in a category. So, one category of variable is called String, which can hold text. So your name would be text. So you can put that in a String type variable. Again, I'm using code hinting. I just have to type the first few letters of string, and then I can select that from the window. Again, capital S, and it should all be blue. When you're declaring a datatype, just about every single datatype is going to start with a capital letter.

There are about three or four that are going to be starting with lowercase letters. So myName, it's a String, which is going to hold the text datatype. Then if I want to set a value, I type space, equals, and then space. Then for a text value, you put that in quotes, which is different from a number, which doesn't need quotes. So I'm going to type open and close quotes. Those are going to be green. Again, this comes from your Flash ActionScript preferences, and then a semicolon.

Now, I'm going to move my cursor back in between the quotes, and I'm going to type my name. You can type your name. The semicolon at the end is just like a period in a sentence. It ends a statement. Now if you want to see if this code is working properly, you can use a trace statement. Remember, a trace statement makes a message appear in the Output window. Again, it's usually used for testing your code.

So we'll go to the next line by pressing Enter or Return, and then type the word 'trace,' all lowercase, and it should be blue when you type it. Again, that's a reserved keyword in Flash. Type some parentheses. You'll notice that I do things like typing open and close parentheses, and then typing open and close quotes at the same time, instead of typing one, and then writing the code in the middle, and then typing the other, I do that so I don't forget to balance out the quotes or braces, because you always need to opening and close quotes.

You're always going to need opening and close parentheses, curly braces, et cetera. So I like to make sure I don't forget to write the closed one by writing it at first; same thing with the semicolon. I'll move my cursor back in between the parentheses by using my arrow keys. Then inside of the trace area, which is the parentheses, I'm going to type myName, just as I typed it above. So now I'm going to test the movie and see what appears in the Output window.

Notice that it says Todd, and it doesn't say myName. So the value of the variable is showing up in the output window, and not the variable name. So I put the name Todd inside of the myName container. There are many different types of variables that you'll be working with throughout the rest of this title. You'll learn more about them as you get more practice using them. But for now, the main thing I want you to remember is that variables are containers that hold data, are comparable to nouns, and are the basic building blocks of ActionScript 3.

Understanding variables
Video duration: 4m 56s 7h 8m Beginner Updated May 23, 2012


Understanding variables provides you with in-depth training on Developer. Taught by Todd Perkins as part of the ActionScript 3.0 in Flash Professional CS5 Essential Training

please wait ...