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.");
}
| 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:
- How to if and else conditional statements in Actionscript 3
- Mouse Events Actionscript 3 in Flash cs3 Tutorial
- Tutorial Create a reusable button class in Actionscript 3 using Flash CS3
- Remove Child () tutorial in Flash CS3 using Actionscript 3
- How to use Event listeners/Handlers in Flash CS3 using Actionscript 3




tredinertok says:
July 11th, 2007 at 10:09 am
Hi
Very interesting information! Thanks!
G’night
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
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
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.
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.
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…