Constructing complex conditional testing in Actionscript 3

Wednesday, June 6, 2007 5:13

 

It often occurs that you want to make a decision based on multiple conditions. For more complex applications and especially in games developments and learning software and can end up writing a lot of code worth of if and else statements here is a solution to simplify this somewhat this tutorial is developed for a beginner to intermediate programmer so


any advanced programmers may need to check through the more advanced sections

The solution is simple use the logical AND (&&), OR (||), and NOT (!) operators to create compound conditional statements. The operators are set as a sort of short hand to make it simpler and more convenient whilst scripting.

How its done in Action Script 3

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 20th.
var current:Date = new Date(  );
if (current.getDate(  ) == 20 && current.getMonth(  ) == 3) {
  trace ("Happy Birthday, Alex!");
}

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

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

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

// Check if it is a weekend.
if ((current.getDay(  ) == 0) || (current.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 Alex.
if (!(userName == "Alex")) {
  trace ("This application knows only Alex's birthday.");
}

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

if (userName != "Alex") {
  trace ("This application knows only Alex'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 sprite is visible. If so, display a
// message. This condition is shorthand for _sprite.visible == true
if (_sprite.visible) {
  trace("The sprite is visible.");
}

The logical NOT operator is often used to check if something is false instead of true:

// Check to see if a sprite is invisible (not visible). If so, 
// display a message. This condition is shorthand for 
// _sprite.visible != true or _sprite.visible == false.
if (!_sprite.visible) {
  trace("The sprite 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 (!((userName == "Alex") || (userName == "Tom"))) {
  trace ("Sorry, but only Alex and Tom have access to this application.");
}

ActionScript 3 doesn’t 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 3 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. The code stays set out much clearer and easier for others to read and find out what’s going on. Very consistent with many multiple testing sequences you may construct

web site
buytolet mortgage
uk mortgage
compare products

 

 

If you liked this tutorial then click the donate button


Subscribe to Miraz Tutorials by Email

 


Similar Posts:

Share/Save/Bookmark

You can leave a response, or trackback from your own site.

6 Responses to “Constructing complex conditional testing in Actionscript 3”

  1. tredinertok says:

    July 11th, 2007 at 10:09 am

    Hi

    Very interesting information! Thanks!

    G’night

  2. Daniel says:

    August 2nd, 2007 at 1:55 pm

    I have to say, that I could not agree with you in 100% regarding cting complex conditional testing in Actionscript 3 | MirazTutorials.com, but it’s just my opinion, which could be wrong :)

  3. shaz says:

    August 3rd, 2007 at 12:29 am

    Great stuff Daniel everyone is entitled to their opinion however maybe you can enlighten me as to what aspects of the topic you dont agree with that way we can maybe increase upon one anothers understanding

  4. Oliver Turner says:

    August 7th, 2007 at 9:45 am

    If I were Daniel I’d say…

    if this is a tutorial on complex conditional testing, what about the switch statement? Much the most flexible option.
    http://www.tech-recipes.com/rx/2369/as3_switch_case_statement_syntax

    Otherwise, what about the ternary operator? Nice and concise for simple conditionals.

  5. Daniel says:

    August 19th, 2007 at 8:12 am

    I couldn’t understand some parts of this article cting complex conditional testing in Actionscript 3 | MirazTutorials.com, but I guess I just need to check some more resources regarding this, because it sounds interesting.

  6. shaz says:

    August 19th, 2007 at 8:54 am

    Oliver seems your a little far ahead of me! maybe you can teach me a few more things some day. well done…

Leave a Reply