actionscript 3.0 for journalists

Variables and Syntax

When learning any spoken language, some of the first things that are generally covered are basic punctuation and grammar rules. The same could be said with programming languages. ActionScript follows some universal conventions that apply to the majority of programming languages.

Due to the strictly-typed nature of ActionScript 3.0, most of these example will not work just yet if you attempt them in Flash. This part of the tutorial is meant to be read without performing the examples. It will build up to the point where the examples will be meant to follow along using your own copy of Flash.

Using variables to store information

A variable is one of the most basic and fundamental parts of coding. Much like algebra, a variable is a letter or a word used to represent a value. Take the following example:

x = 7

In this example we have the letter x representing a number, 7. We know that every time we see the letter x, that it really stands for a number.

The equals character, not what you think

There is a minor but very important difference between algebraic expressions and programming expressions. That difference lies in the equals symbol. In algebra, a single equals character (=) represents an equivalency. It means both sides of the equals character must be the same. However, this is not the case with programming. In most programming languages, including ActionScript, a single equals symbol represents something called the assignment operator. An equals character takes the value that is on the right side, and assigns it to the variable on the left side of the equation. Take the following example:

x = 7 
k = 15 
x = k

What is the value of x? If this were an algebraic expression, this would simply be "not true" or "false" because it is obvious that x and k have different values. However, if this were a programmatic expression, the value of x would be 15. Why? Let's start from the top. x = 7, check. k = 15, got that. x = k, in this last line of code, we are replacing the value of x, with the value of k. Any time a single equals character is used, it assigns a value from the right side to the left side.

Semicolon; The period of ActionScript

In ActionScript, a semicolon (;) is much like a period in the English language. It ends each statement and tells Flash to move on to the next statement of code. However, one difference between English and ActionScript is that typically every "sentence" or statement of code, starts on a new line. While this is not a requirement, it is a best practice that allows the code to stay readable.


i = 7; 
k = 15; 
age = 21;

Notice how each line of code has a semicolon at the end. There are some exceptions to this rule that we will get to later. Also notice the variable age. Unlike algebra, variables are almost always words in ActionScript, not just single letters. This is a good practice so that we know what is stored in the variable. In fact, this will be the last time we use a single letter as a variable in this tutorial.

Variables, store more than just numbers

Variables can store more than just numbers. In fact, in ActionScript you can use a variable to store pretty much anything in the program (more on that later). For now, it is important to know how to store different datatypes. In this example, we can see that we can store words or numbers:

age = 21; 
firstName = "John"; 
lastName = firstName;

If we want to assign a number to a variable, the process is pretty basic. But if we want to store a word (also called a string), you will need to put quotes around the word to indicate that you want to store the literal word itself – in this case the word "John". Without quotes, Flash will think you want to assign the value of another variable. For example, in the last line of code from above, the variable lastName stores the value of another variable, because there are no quotes around firstName. Just remember, putting quotes around a value represents the literal, or intrinsic value, of the word. No quotes means Flash will think it is variable.

Colon, specifying a datatype

The colon character is something that is often used with ActionScript 3.0. The colon (:) tells Flash the type of data that is to be stored in a variable. This part will require you to first learn a few of the different datatypes. (Also note that datatype names are case sensitive and are almost always uppercase) A Number is just what is sounds like, a numeric value. A String is a word or sentence in quotes. A Boolean is a datatype with only two possible values, true or false. A Boolean is named after George Boole, the inventor of Boolean Logic.

age:Number = 21; 
firstName:String = "John"; 
lastName:String = "Smith"; 
oldEnoughToDrink:Boolean = true;

Notice the placement of the colon. The colon is used to describe the type of data of the variable preceding it. You only have to include the colon the first time you use each variable. After that, you will not have to use the colon for that variable again.

Var keyword, used to declare a variable

The first time you use any variable in a Flash program, you must include the keyword "var" in front of it. This is called the declaration. By declaring a new variable, you are telling Flash the name of your new variable and how you intend to use it (by using the colon character from above). When you declare a variable, Flash will do a bunch of technical things like allocate memory for your variable.

var age:Number = 21; 
var firstName:String = "Jane"; 
var lastName:String = "Smith"; 
lastName = "Doe"; age = 22;

Pay attention to the last two lines of code in the example above. We have reassigned the lastName variable. Maybe Jane Smith remarried and changed her last name to Doe. Since we have already declared the lastName variable, we no longer need the var command and the colon and datatype.

At this point in the tutorial, the code above would actually work within Flash. If you do not declare a variable the first time you use it, Flash will likely throw an error.

CamelCase and other variable naming rules

You might have noticed by now that many variables are actually multiple words smashed together with no spaces. We capitalize each new word in the phrase in order to make it readable and eliminate ambiguities. This convention is called camel case. It allows us to make variable names specific enough so that we know what information it stores. Variables always start with a lower case letter. We do this so it won't be confused with datatypes and something called Classes, which are always uppercase. (more on that later).

These are the rules for variable names:

  1. No spaces in variable names. Best practice is to use camel case for different words
  2. Must start with a letter. Though, you can have numbers after the first character.
  3. No symbols. Only letters, numbers and underscores ( _ ) can be used.
  4. Cannot use a variable name reserved by Flash. If your variable turns blue when you type it, that word has special meaning in Flash and is reserved. For example, Flash reserves the word text. Try using something like myText or descriptionText instead.