If you are a beginner when it comes to development, the concept of an if/else statement is probably something you know without actually realizing that you know it. It is one of the simpler theories of development that you can learn.
If statements are logic processes that we use every day to make decisions and the logic that applies to those types of real life situations is the exact same logic that applies to Actionscript. For example, if you are hungry, you go get something to eat. Breaking a statement like that down into Actionscript is very easy. Take a look at the following code.
-
-
if(hungry == true){
-
this.gotoAndPlay("timeToEat");
-
}
-
In this example assume that we have a variable named ‘hungry’. In our if statement, we check to see if hungry is true. If it is true, then we go and play the ‘timeToEat’ label.
The basic idea of an if statement is to compare two variables. In the example, we compare the variable ‘hungry’ to the value ‘true’. There are a few other functions you are able to use if statements for, but we will get into those later on.
This double equal sign is called an Operator, and there are a number of different operators available for you to use in your if statements. The operators tell the if statement how it should compare the two variables. With operators, you must keep in mind that whatever code you put within the curly braces will be executed if the if statement is true.
|
Operator
|
Description
|
|---|---|
|
==
(equals) |
Compares the two variables and if they are the same, then the code will be executed.
Example: Code (actionscript)
|
|
!=
(does not equal) |
Compares the two variables and if they are not the same, then the code will be executed.
Example: Code (actionscript)
|
|
<
(less than) |
If the left variable is less than the right variable, the code will be executed.
Example: Code (actionscript)
|
|
<=
(less than or equal to) |
This acts the same as the less than operator, with the exception that the code will be executed if the two variables are the same.
Example: Code (actionscript)
|
|
>
(greater than) |
If the left variable is greater than the right variable, the code will be executed.
Example: Code (actionscript)
|
|
>=
(greater than or equal to) |
This acts the same as the greater than operator, with the exception that the code will be executed if the two variables are the same.
Example: Code (actionscript)
|
The equals and the not equals operators are not always required. When working with Boolean variables (variables that only equal true or false), you are able to use the following code:
-
-
if(myBoolean){
-
trace("myBoolean variable exists and is true");
-
}
-
if(!myBoolean){
-
trace("myBoolean variable either does not yet exist, or it is false");
-
}
-
By leaving out an operator, or adding the ‘!’ in front of the variable name, the if statement knows to check if it is true or false. As you may have noted by the trace statements, the code will also verify that the variable has been defined.
If not working with Boolean variables, you can still use this method to check and see if the variable is defined.
Those are the basic operators, but what happens if you want to do something only if the variable myNumber is greater than 15 and less than 20? Well with what you’ve learned above you could write the following:
-
-
if(myNumber > 15){
-
if(myNumber < 20){
-
trace("myNumber is 16, 17, 18 or 19");
-
}
-
}
-
While this will get the job done, it’s a little excessive and messy. This is where the final two operators can come in to play.
|
Operator
|
Description
|
|---|---|
|
&&
(and) |
The and operator has two basic functions. The first to check if multiple variables are true and the other is to check if they all exist. Much like when we have an if statement with no operator, but this will do multiple variables at the same time.
Example: Code (actionscript)
This example checks to see if both variables are true, and if they are both true, it executes the code in the curly braces. If only one variable is true, the code will not be executed. This same code will also check to see if the variables ‘hungry’ and ‘thirsty’ exist. If either one of those are undefined, the code will not get executed. Now if you change the above code to: Code (actionscript)
The if statement checks to see if the variables are false or undefined, due to the ‘!’ operator in front of the variables. The more advanced functionality of the and operator is to have multiple evaluations in the same if statement. It is a good practice to put brackets around each separate evaluation. Now for this code to execute, the number must be above 15, and less than 20. Example: Code (actionscript)
|
|
||
(or) |
The or operator functions much like the and operator, but only one of the evaluations is required to be true for the code in the curly braces to be executed.
Example: Code (actionscript)
In this example, if your ‘hungry’ variable is true, but your ‘thirsty’ variable is not, you should go check for a snack in the fridge. If only ‘thirsty’ is true, go check for a drink. The or operator can also check if either of the variables supplied are defined. Like the and operator, the or operator also has some advanced functionality. Example: Code (actionscript)
With this example, we compare the current time to the hour that dinner is typically at. If the current time is dinner time than we should go eat, regardless of if we are hungry. Likewise, if we are hungry and its not dinner time, we should probably eat too. With the or and and operators, you can add in as many evaluations as you like to get the job done. |
That pretty much covers the if portion of the If and Else statements, but there is more that can be done here.
Check out this example:
-
-
if(morning){
-
trace("wake up");
-
} else {
-
trace("better not fall asleep yet");
-
}
-
This example now has the ‘else’ statement attached to the end. In simple terms, the else statement offers code to fire if the if statement is not true. Some of our examples from above with an else statement added on:
-
-
if(myNumber < 10){
-
trace("my number is lower than 10. The highest myNumber can be is 9");
-
}else{
-
trace("my number is greater than 10");
-
}
-
-
if(hungry && thirsty){
-
trace("go get a combo from the closest burger joint");
-
} else {
-
trace("do some more if statements!");
-
}
-
As you can see, the logic for the else statement is pretty simple, and we are almost done this tutorial! Just one more aspect of these if/else statements….the Else If…
This one is pretty simple too, after your first if statement, you want to make another if check before you have the all encompassing else statement, you throw in an else if.
Example:
-
-
if(theTimeOfDay == theMorning){
-
trace("wake up");
-
} else if(theTimeOfDay == myBedTime){
-
trace("go to bed");
-
} else if(iShouldBeAtWork){
-
trace("working right now");
-
} else {
-
Trace("watch some tv");
-
}
-
This code first checks to see if it’s the morning so you can wake up, because it’s hard to get much of anything done when you’re still asleep. This is great to wake you up, but we can’t just toss an else statement telling you to go to bed if its not the morning, because what if its lunch time, you don’t want to be sleeping through your lunch break, do you? To prevent lunchtime naps, we put some other if statements in and make some checks for other times of the day. After we make all the checks we would like to make, we have the final ‘catch’ for anything that doesn’t fit into any of the other if statements.
In the above example, if it’s not the morning, and it’s not bedtime, and you shouldn’t be at work, then you might as well kill some time watching tv.
Summary
Hopefully with this you understand the ins and outs of if/else statements. If you have any questions regarding this stuff, don’t hesitate to leave a comment, or make a post on the pixel2life forums under the Flash Help forum.













Subscribe by Email
4 responses so far
1 mike // Oct 30, 2007 at 10:19 pm
what if I wanted to state
[pre]
if(portimage._currentframe ==(105,123,141,159,177,195)){
portimage.nextFrame }
[/pre]
stating that if the number equals 105,123,141,159,177,or 195 to play nextFrame
2 Alex // Nov 1, 2007 at 7:43 am
Mike:
You can use the || to evaluate multiple conditions within the if statement. So you would have:
var curFrame:Number = portimage._currentframe; if(curFrame == 105 || curFrame == 123 || curFrame == 141 || curFrame == 159 || curFrame == 177 || curFrame == 195){ portimage.nextFrame; }Hopefully that helps. It’s a little long, but it gets the job done.
3 Entries Tagged as ‘Flash Tutorials’ « kaZetku // Feb 2, 2008 at 5:57 pm
[…] If and Else statements with Actionscript […]
4 Robin // Jul 26, 2008 at 6:09 pm
How would you write an if statement to change color of a movie clip for example would it be similar to this:
if(status == sold){
mybox_mc.color(”#000000″);
}
Leave a Comment