What Are Variables
Think of variables as labelled jars that contain different types of data. There are many types of variables, but we'll only look at three for now:
String
Number
Boolean
A String is a series of letters, numbers, and some characters that are contained within quotation marks. ("" and ('' can both be used. An example of string might be "Hello! I'm learning variables in Flash today!" Strings are generally used for storing text.
A Number is exactly what you would think it is: a number. You don't store them in quotation marks like strings. Instead, number variables are written as they are. An example a number might be 9. Or 10. Or 32.432. Yes, you can do decimals too.
Lastly, a Boolean is always either true or false. It is sort of like an on/off switch, so you can use a boolean variable to ask true or false questions in your code. For example, if you were making Elephant Quest, you might need to ask "Does the player have the key?" and it was true, you would then open the door.
So how do I put a variable in my code?
A variable in AS3 (Actionscript 3) looks like this:
var myVariable:String = "Hello World!"
The variable has 4 parts in it. The first is the
var at the front. This tells the computer that you are declaring a variable. The second is the name, which in my case is
myVariable. All programming languages have something called reserve words, which means that they can't be used as variable names. In Flash, reserve words will generally change colour when you type them.
The third part is the :String. This tells the computer what you want to put in your variable, in this case a String. When you type this part, make sure to put a colon (
in front, and capitalize the word.
The last part is what is in the variable, in this case, the text "Hello World". Note that you do not have to do this and you can just type:
var ArmorGames:String;
This just makes an empty placeholder for the variable ArmorGames.
You'll also notice a semi-colon at the end of the thing. This tells the computer that you're done with this statement.
Make sure to remember your semicolons! They're very important!Now to actually use a variableNow you know how to make variables, but you can't do anything with it yet. Now we will use these variables in the trace() function.
Open up your copy (or trial) of Adobe Flash. For this, I will assume you're using CS4 or CS5.
Now, click on the ActionScript 3 button. You should now have a new window open in Flash. On the bottom (or top if you're using CS3) of the screen, there should be numbers, and a weird rectangle with a circle in it under the number 1. This is called the
Timeline.
Right click on the first rectangle thingy (called a Frame). This will open up the right click menu. Down the bottom of the list, there should be a button that says "Actions". Click on it.
The box you see now is where you do the actionscripting. Each frame has it's own box, so you may have different code in different frames. For now, we'll be only using one frame.
Type (DO NOT COPY) the following code into the box:
var myVariable:Number = 5;
You do not need to call it myVariable, so feel free to call it what you like.
Let's have a look at other variable types we can declare. Type the following:
var myString:String = "Hello World!";
var myBoolean:Boolean = false;
Now you should have 3 variables declared. Now type the following:
trace(myString);
You'll notice we've added in the line trace(myString). This makes what you put in the brackets appear in the Output box. (Don't worry if you don't know what the Output box is yet.) The parenthesis(brackets) following the word
trace allow you to provide data the computer might need to do whatever. In this case, we gave it myString, so the computer should output "Hello World!".
Your box should now look like:
Now test your movie. Go to Debug --> Debug Movie --> In Flash Professional or just press Ctrl-Enter.
If you did it right, the words "Hello World!" should appear in the output box.
Try putting the other variables in the brackets and see what happens!
Still don't know what the Output box is? Click here.Help! It didn't work!This is the debugging section of the lesson. 9 out of 10 times, your problem will be answered here.
If you got the error 1120, Access of Undefined Property, then:Make sure you spelt and capitalized the variables right. For example, you might of done MyString instead of myString. Any little thing like that causes Flash to choke.
If you got the error 1180, Call to Possibly Undefined Method, then:Make sure you spelt trace right.
Next one coming soon!