A variable is a little bit of memory, it'll hold a value defined to it. They're used for accessing later, so you can work with its information. AS1.0 and AS2.0 work with variables in the same fashion, except for one major difference. I'll be sure to point that out later. To create a variable, you'll use the following syntax:
var myString = "Hey";
Notice the "var"? That's announcing the variable. The word following it is the variable name. In the above case, I assigned the value "Hey" to the variable. The ending semi-colon announces the end of the line (The semi-colon is required).
AS2.0 Only:
Variables in AS2.0 have one extra feature. Datatype definition.
var myString:String = "Hey";
Notice the ":String"? That's the Datatype. When you go to type the above in, after inputting the colon, a list will drop with all the Datatypes. Look through it sometime.
Alright, now that you know how to assign variables, I'll introduce you to a method commonly used in Flash. The trace() method.
var myString = "Hello, this is a variable"; trace("The variable's value: " + myString);
Put that into Flash's Action's panel, and press "Ctrl + Enter". You'll notice the output box. That is caused by the trace() method, as it is used to display data. If you haven't already guessed, the value to by outputted is defined inside the parenthesis.
Also, the "+" Operater can be used to combine to entities. In this case, a String and a variable. So, you now know how to create variables and output them. As you continue with Flash, you'll find more and more used for them, this was just a quick intro :)
Comments
Post new comment