Using conditions and loops, you can teach your Flash animations how to make decisions, and viewers will think both you and your animations are very smart. ActionScript provides a few different decision-making statements that you can use depending on your needs. In human-ese, they work like this:
If (this condition exists) do (these actions)
While (this condition exists) do (these actions)
For (X number of times) do (these actions)
Those three forms of decision-making may seem very similar, but as you'll see in the following explanations and examples, there are some important basic differences. You'll hear programmers refer to these decision-making statements using different terms, like program flow controls, conditionals, and loops. These tools have one thing in common: They all help you dictate whether or not ActionScript runs some specific lines of code.
Two of ActionScript's statements tackle the "If (this condition exists) do (these actions)" situations. The first and simplest state is appropriately called an if statement.
The most basic if statements are built like this:
if (this is true) {do this}
So, if you're writing an ActionScript statement for a parking meter cop, it might look like this:
if (parkingMeterExpired==true) {writeTicket();}
It works like this: If the condition within the parentheses is true, then your code performs the statements within the curly brackets. If the expression in the parentheses is false, then your code ignores the statements within the curly brackets. In this example, if the parking meter has expired, the cop writes a ticket. If the parking meter hasn't expired, the cop doesn't do anything. (Well, maybe she goes for donuts.)
The parentheses () after writeTicket are part of any function including the writeTicket() function. The following semicolon is the proper way to end a statement.
In some cases, you may want to provide some additional alternatives. Suppose you're sending your assistant to the auto store. You want him to buy a Stutz Bearcat, but in the unlikely event that the store doesn't have any Bearcats, you want him to buy a Packard Roadster. In ActionScript, that instruction takes the form of an if…else statement.
if (storeHasStutzBearcat==true) { buyStutzBearcat(); } else { buyPackardRoadster(); }
You can string if…else statements together to handle several different conditions. The result looks like this:
if (storeHasStutzBearCat=true) { buyStutzBearcat(); } else if (storeHasPackardRoadster=true) { buyPackardRoadster(); } else if(storeHasHudson=true) { buyHudson(); }
There's a statement that should resolve any out-of-stock issues at the car store. ActionScript works through the statements from top to bottom. When it finds a car in stock, it buys the car. Using this if…else if structure, your assistant will purchase only one car. For example, you won't end up with both a Packard Roadster and a Hudson. If the Packard is in stock, the function buyPackardRoadster() runs and the else if(storeHasHudson…) portion is ignored. If none of the conditions are met, then your assistant buys no car.
You can string together as many if…else if conditions as you want, but at some point the code gets a little awkward and hard to read. The switch() statement makes a good alternative when you have more than three conditions to check. Suppose you want to create a system where a parking ticket costs more depending on the number of tickets the scofflaw has received. The variable numberOfTickets holds a Number value. A switch() statement might look like this:
switch (numberOfTickets) { case 1 : parkingTicket = 25; break; case 2 : parkingTicket = 50; break; case 3 : parkingTicket = 75; break; case 4 : parkingTicket = 100; break; case 5 : parkingTicket = 125; break; default : parkingTicket = 0; }
The switch statement takes the variable numberOfTickets and, starting at the top, compares it to the first case. If the offender has a single parking ticket, then the value of parkingTicket is set to 25. When ActionScript gets to the word break, it jumps to the end of the switch() statement and doesn't run any of the other cases. The original case is used when none of the other cases match.
There's a good rule of thumb to remember with computers. If you're doing the same chore over and over again, there's probably some way your computer can to do it more efficiently. Computers are great at repetitive tasks, and that's certainly true of ActionScript, which has two great ways to repeat statements in your programs. Using the while() statement, you can have your computer repeat a task as long as a certain condition exists. Using the for() statement, you can tell your computer exactly how many times to repeat a task.
The while() statement checks to see if a condition is met. If it is, then the code within the curly brackets runs. If the condition isn't met, then ActionScript moves on to the next statement. It's very common to use the while() statement with a variable that's incremented. So, if you want your assistant to pop down to the auto store and buy six Stutz Bearcats (enough for you and a few close friends), you'd put together a statement like this:
while (myStutzBearcats < 6) { buyStutzBearcat(); myStutzBearcats = myStutzBearcats + 1; }
Like the if() statement, the condition for the while() statement is inside parentheses, and if the condition is met, then the statements inside curly brackets {} run. In this example, the second line runs a function that buys a Bearcat. The third line increments the variable that keeps track of how many Bearcats you own. As long as that number is less than 6, the program loops; you buy another and add 1 to the number of Bearcats you own.
The whole idea of incrementing a value, like myStutzBearcats, is so common that there's even a shorthand way of adding one to a variable. The third line could also read:
myBearCats++;
There's no assignment operator (=), but the statement is assigning a new value to myBearcats. When you want to count down, you can use the decrement operator (--). Here's a statement that sells off your Stutz Bearcats until you have only three left.
while (myStutzBearcats > 3 { sellStutzBearcat(); myStutzBearcats--; }
If, as you're reading ActionScript code for pleasure and you come across i++, don't be surprised. The i usually stands for iterator or integer. It's the programmers' way of saying, "I need an integer to operate this loop, but it can be any old integer, it doesn't need a fancy variable name." Using the lowercase i as a stand-in isn't a rule, just a programmers' tradition.
The for() loop gives you a very compact way to repeat a portion of your program. The mechanics that make a for() statement run are all packed in the first line. It's so compact and down to business that it reads a little more like machine talk than human talk. Here's an example:
for (var myStutzBearcats:Number = 0; myStutzBearcats < 6; myStutzBearcats++) { buyStutzBearcat(); }
Like the earlier statement, this loop buys six Stutz Bearcats. A lot goes on in that very first line of code. From left to right, here's what happens:
The word for indicates a for() statement.
The beginning parenthesis is your clue that the condition follows.
The word var means that a variable is being declared.
myStutzBearcats:Number is the name of that variable and its data type.
The assignment operator (=) immediately gives the variable a value of zero.
Following the semicolon, you finally arrive at the condition that's being tested: Are there fewer than six Bearcats?
The semicolon ends that statement, and in the next myStutzBearcats is incremented.
The for() statement is very compact, and it puts everything you need to know right up front. Script writers often create for() statements using the variable i for the condition (see the note on Note). You'll often see something like:
for (var i = 0; i < 6; i++) { buyStutzBearcat; }
Just like the earlier for statement, this one purchases six of those beautiful Bearcats.
For an example of the for() statement in action, download 12-2_For_Statement.fla from the Missing CD page (www.missingmanuals.com/cds).