Bookmark now!

AddThis Social Bookmark Button

Most popular

The most viewed tutorials in April:

  1. Best dynamic image gallery
  2. Loading external JPG images
  3. Ultimate preloader

Advertisements

Amazing video loops, stunning photo galleries, video and audio players ready to go! Smooth, amazing flipNavigation system!

Learning to use the conditional if/else statement in Flash 8 - part 2

This page of tutorial on conditional logic in ActionScript continues where the previous left off. Save your document if you haven't already. Make this a habit, it will save you much frustration.

Explaining the if/else conditional logic

16. Add the following to your existing code (bolded):

roomLight_mc._alpha = 0;
var lightOn:Boolean = false;
roomLight_mc.onPress = function() {
if (lightOn == false) {
roomLight_mc._alpha = 100;
} else {
roomLight_mc._alpha = 0;
}
};

Note: if confused as to where you should add the above code or if you get errors while trying to test your movie, just copy all of the code above and paste it over the existing code, erasing any previous.

Here you have the if/else conditional statement, which gives you more options and possibilities than the if statement alone. It works like this:

A user clicks the movie clip. Flash runs the function and in the exact same way as before checks out if the circumstance being presented evaluates as true. It evaluates as true and the light appears (the movie clip containing the lamp image is displayed). And the else part of the conditional statement gets completely ignored. Why?

Because the else part of the conditional statement would be executed only and only if the circumstance evaluated to false. Translated to human language, an if/else statement works like this:

if (this circumstance evaluates as true) {
... this code gets executed ...
} else {
... and this code here is ignored and skipped altogether.
}

Let's delve deeper into the possibilities that this offers you. Let's make possible for the light to be turned on and off.

17. Append this to your code (shown in bold):

roomLight_mc._alpha = 0;
var lightOn:Boolean = false;
roomLight_mc.onPress = function() {
if (lightOn == false) {
roomLight_mc._alpha = 100;
lightOn = true;
} else {
roomLight_mc._alpha = 0;
}
};

As you may have noticed, there aren't any var or Boolean keywords in this new line that you just added. The reason why is that once you have defined a variable and its type, you don't have to do it ever again. From the moment you defined it properly (as you did in this lesson), Flash is aware that this variable exists and also of its type.

18. This simple line of code enables Flash to realize that light has been turned on. So, the execution of the else part of the conditional statement will be possible. Try it:

Well, let me explain you: When you clicked on the movie clip the first time, the circumstance was evaluated. It turned out to be true and so the first part (the if part) of the conditional statement was executed, setting the alpha property of the movie clip to 100 and also setting the value of the lightOn variable to true.

When you have clicked the movie clip again, the evaluation process was set in motion again. This time, the circumstance evaluated as false. Therefore, the else part of the statement got executed, putting the alpha property of the movie clip back to zero and plunging everything into darkness.

But on the third click, nothing happened. Why wasn't the light turned on again? Simply because the value of the lightOn variable stayed true. Therefore, upon evaluation, the circumstance turned out as not true, meaning the value of lightOn is not set to false, but true, which makes Flash skip the if part and run the else part instead.

Once the else part is run, it sets the alpha property of the movie clip to zero again. It was already set to zero during the second click, so there is no change. It stays on zero, with the being movie in darkness.

KEEP IN MIND Flash can not understand by itself that a change has happened - that light has been turned off. You, as a human being, see that. But Flash needs you to tell it that it happened, by changing the value of the lightOn variable back to false again. Computers are very, very stupid machines. They have no intelligence at all. They just have the incredible capacity to perform an enormous number of calculations in a fraction of a second. And that's it. Forget the artificial intelligence as portrayed in the sci-fi movies. It will be long since that kind of computing will see the light of day. Until that happens, stick with me and enjoy learning more Flash ;)

19. Make the final addition to your code (bolded, again):

roomLight_mc._alpha = 0;
var lightOn:Boolean = false;
roomLight_mc.onPress = function() {
if (lightOn == false) {
roomLight_mc._alpha = 100;
lightOn = true;
} else {
roomLight_mc._alpha = 0;
lightOn = false;
}
};

20. Test your movie again (Ctrl+Enter). Click until you can't click no more. The light goes on and off as intended. All thanks to a simple conditional logic statement. For a better understanding, here is a nice graph that explains it all:

The flow in the if/else conditional statement.

Top of page

Comparison operators used in conditional statements

Let me show you now some frequently used operators. These operators are also called comparison operators. That's because their purpose is to compare values - to tell if one equals another, is greater or lesser then the other one etc.

To see if a value is lesser (smaller) then some other value, you must use the lesser than (<) operator, as in the following example:

var numberOfApples:Number = 4;
if (numberOfApples < 7) {
buyMoreApples();
}

The result of the evaluation of the circumstance (also called a condition, remember) would yield as true, because 4 is lesser than 7. In much the same way, you could test if a value is greater than some other one, using the greater than (>) operator, like this:

var numberOfApples:Number = 4;
if (numberOfApples > 7) {
makeApplePie();
}

The circumstance in the example above would get evaluated as false, because 4 isn't greater than 7. Besides the two already mentioned operators, you can also test if some value is lesser than or equal to (<=) some other value or greater than or equal to (>=) whatever value it is being tested against.

Throughout this tutorial, you have used the equality operator (==) to see if a value equals some other value. On the other side, the inequality operator (!=) is used to test if something is not equal to something else:

var today:String = "Thursday";
if (today != "Sunday") {
gotoWork();
}

The string (a string value is a text value) "Thursday" does not equal "Sunday", therefore, the evaluation would be true and the function gotoWork() would be run.

But what if there are several circumstances that should be checked out? Here is where the logical AND (&&) and the logical OR (||) operators come into play. Suppose that you had to test if the current day is not sunday and that it is past 8 o'clock. You would do it like this:

var today:String = "Thursday";
var hour:Number = 9;
if (today != "Sunday" && hour > 8) {
gotoWorkQuickly();
}

The logical AND operator (&&) makes it obligatory for both tests performed to be true, in order for the whole circumstance to be true. In the above example, it would be enough for just one test to result as false for the whole circumstance to be false: either the value of today equaling "Sunday" or the hour value being lesser than 8. Since both conditions result as true, the whole circumstance yields as true and the gotoWorkQuickly() function gets executed. This is how this kind of situation gets interpreted by the computer:

if (today does NOT EQUAL Sunday AND the hour is GREATER THAN 8) {
gotoWorkQuickly();
}

When in need of testing if just one of many circumstances exists as a precondition for something to happen, use the logical OR operator (||):

if (price > 100 || shoeNumber < 8 || color != "blue") {
dontBuyShoes();
}

In this case, it would be enough for just one condition to be true for the whole circumstance to yield as true. The evaluation would proceed like this:

if (price is GREATER THAN 100 OR shoeNumber is LESSER THAN 8 OR color does NOT EQUAL "blue") {
dontBuyShoes();
}

Well, that wraps up this lesson. In the future, I will also make a tutorial on the if-else if-else conditional statement as well as the switch/case one. Use the knowledge learned in this lesson to make movies that are more interactive which in turn give your users more choices and make their experience more interesting. Also, check out other quality ActionScript tutorials made by me. Keep on ActionScripting!

Download the source FLA file for the example shown at the start of this lesson

Got any comments or questions? Want to add something but don't know how? Discuss it in the forum!