Conditional Statements in ActionScript

by Milan Midovich.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on flash  

You are here: Categories » Computers and technology » Flash

If 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.

Leave a comment or ask a question
Total comments: 0

Flash Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
ActionScript: Repeating an Operation Many Times - You want to perform a task multiple times within a single frame. Use a loop to perform the same task multiple times within a single frame. For example, you can use a for statement: (more...)
Avoiding Conflicting Variables in ActionScript - You want to make sure that variables within a function do not interfere with variables in other functions or within the timeline in which the function is defined. Use the var keyword to declare loc (more...)
How to Exit a Function in ActionScript - You want to exit a function. Functions terminate automatically after the last statement within the function executes. Use a return statement to exit a function before reaching its end. The (more...)
Move Clip Rotation - A property like _x and _y is the movie clip property _rotation. The _rotation property accepts a value in degrees. A circle is divided into 360 degrees. The values used by _rotation range f (more...)
Targeting Flash Movie Clips - The simplest way to target a movie clip is to use its name, followed by a dot, followed by the command you want to send. However, there are plenty of other ways to target a movie clip as we (more...)
Using Mathematical Operators in ActionScript - You want to modify something over time, such as the rotation or position of a movie clip. Use the compound assignment operators to change a variable or property in increments. Or, if increm (more...)
How to build an animated button using Flash - Time: 15 Minutes. Difficulty Level: Intermediate Requirements: Flash 8 Assumed Knowledge: Basic action (more...)
Enhancing Standalone Projector - You want to create an enhanced Standalone Projector with features such as borderless playback, custom titles, no Flash menus, and so on. Use a third-party tool such as SWF Studio or SWFKit to c (more...)
Creating Flash Buttons - Buttons are one of the three main types of symbols that you use in Flash. The others are movie clips and graphics. Making a New Button There are many ways to create a button. One (more...)
Bouncing Ball Script - Start a new Flash movie. Create a movie clip that has a ball graphic inside it. You can name the instance of the movie clip, myClip, but our code will not depend on the name of the clip. (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.