The 'if' Conditional in ActionScript

By Riyadh Al-Balushi (Blue_Chi) | Flash ActionScript | Beginner

Conditionals are special tools in ActionScript that allow us to execute a specified selection of code upon the satisfaction of a certain condition. The 'if' conditional is one of the several conditionals available in ActionScript and is one of the easiest ones to use. This tutorial will teach you everything you need to know to start using it in your projects. You are required to have some basic ActionScript knowledge to follow this tutorial.

As stated earlier, a conditional is a tool that lets us execute a certain code only if a condition that we specify is satisfied. For example, in a flash game you can let the player move to the next level if he reaches a score of a 100 or higher, in another situation, you can allow a user to access a certain section in a website only if he submits the correct password for that section.

The 'if' conditional statement could be broken into two parts, the first is the condition that needs to be satisfied, and the second is the collection of statements to be executed if the condition is satisfied. It needs to be written in the following structure:

if (condition){
statements;
}

Notice that the condition needs to be written between two normal brackets (), while the statements need to be written between two curvy brackets {}. The condition could be any valid statement that the Flash player could consider as true or false. A basic example would be:

if (score>100){
gotoAndStop(10);
trace("You Won!" );
}

In the upper example the message 'You Won!' would pop up if the value of the score is greater than 100, if the score is not greater than 100, then nothing would happen. It is possible to use other comparison operators such as 'smaller than' (<), 'greater than or equal to' (>=), 'smaller than or equal to' (<=), 'equal to' (==) [Notice that this operator is two equal signs and not one], and 'not equal to' (!=). There are other operators that you could use, but the ones that we mentioned are the most common ones.

In addition to the ability to execute a selection of commands if the condition is true, it is also possible to execute another set of commands if the condition mentioned is not satisfied. This is where the else conditional comes in. This conditional is used after the if statement to execute an alternative set of statements if the condition is not satisfied. Here is the structure at which this needs to be written in.

if (condition){
statements to be executed if condition is satisfied;
} else {
statements to be executed otherwise;
}

Again, the alternative set of statements needs to be written between curvy brackets {}. Notice that you can put any number of statements to be executed. Here is another game example:

if (score>100){
gotoAndStop(10);
trace ("You Won!");
} else {
gotoAndStop(9);
trace ("You Lost!");
}

Another important related conditional that you need to learn about is the else if conditional, which makes it possible to execute a certain collection of commands, if the first condition IS NOT satisfied AND the second condition IS satisfied.

if (score>100){
gotoAndStop(10);
trace ("You Won!");
} else if (score>50){
gotoAndStop(9);
trace ("You lost, you should try again!");
} else {
gotoAndStop(9);
trace ("You completely stink, do not bother to play again!");
}

It is also possible to have more than one required condition or alternative conditions using the 'and' (&&) and the 'or' (||) operators. Using the && operator would let the statements to be only executed if BOTH the first and the second conditions are satisfied.

if (time==0 && score>100){
trace ("You Won!");
}

In the upper example, the player will see the "You Won!" message only if the time equals Zero AND his score is more than a 100, if the time did not finish, or the player did not score more than a 100, the player does not win. On the other hand, if we use the or (||) operator, satisfying any one of the conditions will be sufficient to execute the statements. Notice the difference here:

if (time==0 || score>100){
trace ("You Won!");
}

In this example, the player will see the "You Won!" message if the time runs up or if he scores more than a 100. This is different from the previous one in that the player will not need to wait for the time to finish to win the stage, and he will not need to score a 100 to win if he survives until the time runs up.

Those are all the basic ideas that you need to learn to be able to use the 'if' conditional, it is not difficult at all. I hope that you learnt something helpful from this tutorial. You can view the "The Easiest Method for Creating a Password Protected Section in Flash" Tutorial if you would like to view an exam of the 'if' conditional in practice. Feel free to post at the Oman3D Forum if needed any help.

- End of Tutorial.