Conditions

I l @ ve RuBoard

Now that you know how to compare variables , you can use this information for something besides sending "true" and "false" to the Output window.

The if Statement

The if statement allows you to use the results of a comparison to change the way the Flash movie works. Here is a simple if statement that compares a to see whether it is 7 and jumps to another frame if it is.

 if (a == 7) {     gotoAndPlay(10); } 

The if statement starts with the word "if," followed by a comparison. Always place parentheses around the comparison. Then there is the open bracket .

The next lines, until the close bracket, contain the code to be executed if the comparison is true.

else

You can also include an optional extension to the if statement that executes some code if the condition is not met. Here is an example:

 if (a == 7) {     gotoAndPlay(10); }  else {     gotoAndPlay(15); } 

You can also extend an if statement even further with else if clauses:

 if (a == 7) {     gotoAndPlay(10); }  else if (a == 8) {     gotoAndPlay(15) { }  else if (a == 13) {     gotoAndPlay(20); }  else {     gotoAndPlay(25); } 

You can make an if statement as long as you want. You can even compare different variables in the else if clauses; there is no restriction to keeping it to a similar comparison.

Compound Comparisons

You can also compare more than one thing in an if statement. Suppose that you wanted to go to a frame only if a was 7 and b was 15. You could do that this way:

 if ((a == 7) and (b == 15)) {     gotoAndPlay(20); } 

The and operator takes two comparisons and combines them, returning true only if they are both true. Place parentheses around both comparisons individually to make it clear how Flash should interpret them.

You can also use or to combine two comparisons, but return true if either one or the other is true.

 if ((a == 7) or (b == 15)) {     gotoAndPlay(20); } 

In this code, the movie jumps to frame 20 if a is 7, or if b is 15. If both are true, it also jumps to frame 20. However, if a is not 7 and b is not 15, the gotoAndPlay command is not executed.

I l @ ve RuBoard


Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net