How to if and else conditional statements in Actionscript 3
Sunday, July 15, 2007 9:06conditional statements are used by many developers and can really increase the level of interactivity with your application if used properly can create an engaging experience for the users of your application/s. The main kinds of major uses for conditional statements are Games, quizzes, learning Applications. compiled with Flash RIA platform or even say with Flash professional you will be able to create some very interesting projects and be able to impress your clients and users
You will need your ActionScript code to make decisions in most of the projects you create. what these statements do are distinguish whether or not to let sections of your code or application proceed based on conditions that you will set within your code. If you ever need to locate any of the conditional statements for usage you can locate them in ActionScript’s conditional statements: if, switch, or the ternary conditional operator (? :).
Conditional statements allow you to make logical if there is or if there is not type decisions, it is also dependant on the purpose of the action where by a different condition is used, learning where and when to use which of the conditional statements can really only be learned via experience. For example, the if statement is most appropriate when you want to tell a Flash movie to do something only when a certain condition is met (e.g., when the condition is true). When you have several possible conditions to test, you can use the switch statement instead. And you can use Flash’s ternary conditional operator to perform conditional checking and assignment on a single line.
First let’s look at the if statement. Of the conditional statements in ActionScript, the if statement is the most important to understand. In its most basic form, an if statement includes the keyword if followed by the test expression whose truthfulness you want to evaluate to determine which action or actions to execute. The test expression must be in parentheses and the statement(s) to be executed should be within curly braces (you definitely will need to use them when constructing multiple conditions).
This is a simple test checking if the cars name is correct "Honda" This might be used to check whether the user answered a quiz question correctly (here, CarName is a variable assumed to contain the user’s answer). Note that the double equals sign (==) is used to test whether two items are equal. It should not be confused with the single equals sign (=), which is used to assign a value to an item.
if (CarName == "Honda") {
// This trace( ) statement executes only when CarName is equal
// to "Honda".
trace("Correct! 'Honda' is the correct CarName.");
}
so now that’s the code set up
Next you would most probably need to add an else statement, you can add an else clause to an if statement to perform alternative actions if the condition is false. Note that for the trace( ) command is to display any message you set for it when the code is Run so you can check it is working properly. As the trace() function is for your eyes only you may also want to set a showMessage(), Make a call to a method named showMessage( ) that displays an appropriate message depending on whether the user got the answer right or wrong:
if (CarName == "Honda") {
// These statements execute only when CarName is equal
// to "Honda".
showMessage("Correct! 'Honda' is the correct CarName.");
}else {
// These statements execute only when CarName is not equal
// to "Honda".
showMessage("Sorry, you picked the wrong car.");
}
so now when this code is run it will display either of the
showMessage("") texts
If you liked this tutorial then click the donate button
Subscribe to Miraz Tutorials by Email
Similar Posts:
- Constructing complex conditional testing in Actionscript 3
- How to use Math in Flash Professional CS3 and Actionscript 3
- How to use Event listeners/Handlers in Flash CS3 using Actionscript 3
- Mouse Events Actionscript 3 in Flash cs3 Tutorial
- Photo Viewer tutorial in Flash CS3 using Actionscript 3 Part 1



