Thursday, April 5, 2007

Conditional Testing: The Most Important Concept Of Interactivity

This is an absolutely fundamental concept to understand. Without knowing how to test if certain conditions have been met, you have no way of allowing users to interact with your Flash movie. The most important concept in conditional testing is the all-mighty if statement.

If Statements

If statements are very important in programming games. Without them, you absolutely cannot have a game. An if statement allows your game to branch off into different sets of actions, depending on if certain conditions are met. The syntax for an if statement is as follows:

if (condition==true) {
//Take These Actions
}

So try it out. Let’s say you want an error message printed if your character moves off the visible screen.

//If the character’s x position is greater than the stage’s width,
if (_root.character._x>Stage.width) {
//Trace an error message.
trace(“Character is off the stage!”);
}

Now we can use if statements to determine if a condition is true, and act accordingly based on the evaluation of that condition. This is when your logical operators like the comparison operator (==), greater than (>), less than (<), logical AND (&&), logical (OR), and logical NOT or FALSE (!=, !) really come into use.

Else Statements

An ‘else’ statement is simply the statement that is executed if no if statements in a particular set of if statements evaluate as true. For example, if you’re checking to see whether or not the user entered your parents’ names into a text box, you might use an else statement to print messages based on what they entered:

if (Name==“John” || Name==“Susan”){
trace(“Hey, you’re named after one of my parents!”);
}
else{
trace(“You’re not named after one of my parents….”);
}

Remember what the symbol ( || ) means? That’s logical OR. If either “John” OR “Susan” is entered, the message is traced “Hey, you’re named after one of my parents!”. Otherwise, if any other name is entered, a message will say that you’re not named after one of them.

While these examples may not seem useful, they can help you to establish a strong foundation with understanding how to test for conditions. If and else statements will eventually become second nature to you, and you'll see many of them throughout the games that you create.

0 comments: