Join Todd Perkins for an in-depth discussion in this video Working with conditional statements , part of ActionScript 3.0 in Flash Professional CS5 Essential Training.
Have you ever wondered how applications can think and make decisions? Conditional statements allow you to specify a block of code that runs only if a particular condition is met, and power many different parts of code, including artificial intelligence and games. In this application I have here, I can test the movie and press a key to make the boarder move to the right. Now, it doesn't matter which key I press. I am pressing the up arrow now and the Spacebar now; the boarder always moves to the right. What if we wanted to only move him to the right when we press the right key and move him to the left when we press the left key? Let's look at how to do that.
I'll close the window, select the first keyframe of the actions layer and open up the Actions panel. So, here I have some code that adds an EventListener to the stage, listening for the KeyboardEvent.KEY_DOWN event, and then it runs a function call keyPressed. Instead of the keyPressed function, I can use something called the conditional statement to check which key was pressed. To do that, I'll put my cursor above the boarder_mc line of code, then press Return so I can get a space above that code.
Here I'll type the word 'if' lowercase, some parentheses, and then I am going to wrap that code that says boarder_mc.x inside of curly braces. Just for presentation's sake, I can tab the boarder line of code in, just to clean up the code a little bit. In the parentheses after if, I'm going to type the condition that needs to be true in order for the boarder to move to the right.
So, the condition is going to be if the key that was pressed is the right key on the keyboard. I mean right as in the direction right, and also right as a correct. So, if evt, and that's the event that gets received, That's an object that actually comes in to the function whenever a key is pressed, and that contains information like what key was pressed and other information like that. So, evt.keyCode. As soon as I type a K you'll see keycode highlighted in my Code Hinting window, so I'll just press Return and then a space and then two Equal signs.
That's different than one Equal sign. One Equal sign is used to set a value like when you're setting the value for a variable, but two Equal signs is a comparison operator. Instead of being 'is,' like one Equal means, two Equal signs means 'is equal to.' So we use two Equal signs in conditional statements. So, event.keyCode is equal to, and then a space, and then I am going to type the right key command, which is keyboard.RIGHT, all caps.
So, if the keycode of the event is the right key, or in other words is equal to the keycode of the right key on the keyboard, then move the boarder to the right. So let's test the movie now and see what happens. I am pressing the Spacebar. I am pressing the Return key. I am pressing up arrow and down arrow and left arrow; he doesn't move. I press the right arrow and the boarder moves.
So naturally, if we move to the boarder to the right, we should also move him to the left. Let's just copy and paste this whole block so from the closed curly brace of the if statement up to the word if, copy that with Command or Ctrl+C, and then go down right below that curly brace, make sure you are not messing with curly brace for the function, but the curly brace for the if statement, and then paste the code. So, I have two if statements. Now, I am going to change the keyboard.RIGHT to keyboard.LEFT in the pasted code.
Think about what you might change in the boarder_mc line to make the boarder move backwards. Simply change the Plus sign to a Minus sign. We added to its x position to move him to the right; we subtract from the x position to move him to the left. Test the movie, and he moves to right and left.
Now, if the person playing the game presses the right and the left key at the same time, then there's going to be a conflict. So, what we want to do is make it so only one of these blocks of code runs. So, we only move the boarder to the left if the person playing the game is not pressing the Right key. To specify that, click before the second if statement and type the word 'else' and then a space. So, what this means is, first, we check to see if the Right key was pressed, and if not, or in another words 'else,' check to see if the Left key was pressed.
In that case, move the boarder to the left. Now, you can keep having if statements and else if statements as many times as you want, and you'll have like a hierarchy with the else if statements. Finally, you can end a statement with an else statement. You can specify what you want to have if none of the conditions are met, so I'll type the word 'else' and then some curly braces, and inside of the curly braces, I'll type 'trace' and then in quotes, in the trace statement, I'll just type, "you didn't press left or right." So I'll test the movie and left or right moved the boarder, but if I push the up arrow then I get that you didn't press left or right message.
So of course you can run whatever code you want in there, or not have the else statement altogether. Let's look at a different type of conditional statement. I am going to delete the code that I have here inside of the function, making sure to preserve the curly braces of the function. Then instead of using if and else, I'm going to use a conditional statement called switch case. I am going to type the word switch, and in parentheses I am going to type in 'evt.keyCode.' So that's the keycode of the event and some curly braces.
The switch statement allows us to check one bit of data, in this case event.keyCode, and do different things based on the value of that particular piece of data. So when you're working with a keyPressed, this is more effective and requires less code than using an if statement. So with the switch statement, I type the word 'case,' and then I type the value in for the first example, so that's the Keyboard.RIGHT.
So this is a different way of writing that if statement. This is basically saying the same thing: if event.keyCode is equal to Keyboard.RIGHT. Then after that, type a colon. Then go to the next line and then tell Flash what you want to do if the right key was pressed. So, that's boarder_mc.x += 5 and then a semicolon.
Then to optimize a switch case statement, you tell the statement to stop looking for different options by going to the next line and typing the word 'break.' So this is like the first if statement that we created, case it's Keyboard.RIGHT, then move the boarder to the right five pixels. So, let's test this out and see how it works. It works exactly the same as the if statement. So, let's take those three lines, select them all, copy: Command+ C or Ctrl+C, go to the next line, paste the code.
Now you can change Keyboard.RIGHT to Keyboard.LEFT, change the Plus to a Minus and test the movie again. So we can move the boarder right and left now, but the other keys also don't work. If you wanted to do a final else statement, like we did with the other one where none of the conditions are met, with a switch case statement, you use the default command, so we type 'default' and then a colon, and then you can type in a trace statement if you want, or you can type in nothing; it doesn't matter.
So, in the trace statement, I'll put a different key was pressed, and then we end with a break statement. So you can see that this code is a little bit more concise than what we did with the if statement. So sometimes you are going to want to use the if statement, but for something like a keyPressed, the switch case statement is the better way to go.
So, using conditional statements, you can add logic to your code and decide which code to run based on any criteria you choose.
- Understanding the building blocks of ActionScript
- Working with the display list
- Using dot syntax
- Navigating the Timeline
- Creating document classes
- Linking classes to Library objects
- Adding drag/drop functionality to objects
- Creating a slide show
- Loading and running code in an external SWF
- Working with text
- Accessing XML data
- Playing audio and video with ActionScript
Skill Level Beginner
Q: Will the exercise files for this course work with Flash CS6?
A: Yes, the code should work fine. The language has not changed since CS5.