ActionScript: Performing Complex Conditional Testing

by Gabriel Savimbi.

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

You are here: Categories » Computers and technology » Flash

You want to make a decision based on multiple conditions.

Use the logical AND (&&), OR (||), and NOT (!) operators to create compound conditional statements.

Many statements in ActionScript can involve conditional expressions, including if, while, and for statements, and statements using the ternary conditional operator. To test whether two conditions are both true, use the logical AND operator (&&), as follows:

// Check if today is April 17th.
   now = new Date(  );
   if (now.getDate() == 17 && now.getMonth(  ) == 3) {
   trace ("Happy Birthday, Bruce!");
   }

You can add extra parentheses to make the logic more apparent:

// Check if today is April 17th.
   if ((now.getDate() == 17) && (now.getMonth(  ) == 3)) {
   trace ("Happy Birthday, Bruce!");
   }

Here we use the logical OR operator (||) to test whether either condition is true:

// Check if it is a weekend.
   if ((now.getDay() == 0) || (now.getDay(  ) == 6) ) {
   trace ("Why are you working on a weekend?");
   }

You can also use a logical NOT operator (!) to check if a condition is not true:

// Check to see if the name is not Bruce.
   if (!(name == "Bruce")) {
   trace ("This application knows only Bruce's birthday.");
   }

The preceding example could be rewritten using the inequality operator (!=):

if (name != "Bruce") {
   trace ("This application knows only Bruce's birthday.");
   }

Any Boolean value, or an expression that converts to a Boolean, can be used as the test condition:

// Check to see if a movie clip is visible. If so, display a message. This condition
   // is shorthand for myMovieClip._visible == true.
   if (myMovieClip._visible) {
   trace("The movie clip is visible.");
   }

The logical NOT operator is often used to check if something is false, rather than true:

// Check to see if a movie clip is invisible (not visible). If so, display a message.
   // This condition is shorthand for myMovieClip._visible != true or 
   // myMovieClip._visible == false.
   if (!myMovieClip._visible) {
   trace("The movie clip is invisible. Set it to visible before trying this action.");
   }

The logical NOT operator is often used in compound conditions along with the logical OR operator:

// Check to see if the name is neither Bruce nor Joey. (This could also be rewritten
   // using two inequality operators and a logical AND.)
   if (!((name == "Bruce") || (name == "Joey"))) {
   trace ("Sorry, but only Bruce and Joey have access to this application.");
   }

Note that ActionScript does not bother to evaluate the second half of a logical AND statement unless the first half of the expression is true. If the first half is false, the overall expression is always false, so it would be inefficient to bother evaluating the second half. Likewise, ActionScript does not bother to evaluate the second half of a logical OR statement unless the first half of the expression is false. If the first half is true, the overall expression is always true.

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
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...)
ActionScript: Creating Reusable Code - You want to perform a series of actions at various times without duplicating code unnecessarily throughout your movie. Create a function and then call (i.e., invoke) it by name whenever you (more...)
3D Scaling with ActionScript - Although Flash is not capable of real 3D graphics, the kind seen in popular computer games, you can create the illusion of 3D by using scaling. Scaling an object is a great way to give your (more...)
The Dot Syntax - Something else that you will be seeing a lot of as you learn ActionScript is dot syntax. Dot syntax is a way of grouping objects and functions that is used in many object-oriented programming (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.