Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Working with conditional statements

Working with conditional statements provides you with in-depth training on Developer. Taught by Tod… Show More

ActionScript 3.0 in Flash Professional CS5 Essential Training

with Todd Perkins

Video: Working with conditional statements

Working with conditional statements provides you with in-depth training on Developer. Taught by Todd Perkins as part of the ActionScript 3.0 in Flash Professional CS5 Essential Training
Expand all | Collapse all
  1. 3m 22s
    1. Welcome
      1m 23s
    2. Using the exercise files
      1m 17s
    3. Using the function keys
  2. 23m 38s
    1. Adjusting preferences for ActionScript fonts, colors, and formatting
      3m 25s
    2. Changing Flash Player and ActionScript versions in the Publish settings
      1m 35s
    3. Reading and solving errors through the Compiler Errors window
      2m 49s
    4. Using the Actions panel buttons to add and remove comments
      2m 33s
    5. Using the Actions panel to format code
      1m 49s
    6. Using the Actions panel Toolbox
      2m 4s
    7. Seldom-used but helpful functions of the Actions panel
      3m 14s
    8. Understanding code hinting
      2m 3s
    9. Reviewing the Code Snippets panel
      3m 7s
    10. Using help
  3. 45m 50s
    1. Understanding how ActionScript 3.0 code is processed in the Flash Player
      3m 22s
    2. Understanding variables
      4m 56s
    3. Understanding functions
      9m 1s
    4. Understanding events and event listeners
      5m 47s
    5. Working with conditional statements
      9m 49s
    6. Creating arrays and vectors
      6m 50s
    7. Using the For command to create a loop
      6m 5s
  4. 49m 9s
    1. Reviewing the display list
      3m 0s
    2. Understanding instances referenced through ActionScript
      2m 27s
    3. Using dot syntax to modify properties in an instance
      3m 25s
    4. Placing objects at the center of the stage
      4m 2s
    5. Placing objects at the edges of the stage
      5m 53s
    6. Using the methods of an instance
      3m 44s
    7. Accessing the parents, children, and grandchildren of objects
      5m 50s
    8. Creating instances from Library movie clips using ActionScript
      4m 23s
    9. Adding objects to the stage and changing parents with the addChild method
      5m 45s
    10. Removing objects from the stage with the removeChild method
      4m 17s
    11. Using the numChildren property to loop through a container's child objects
      3m 17s
    12. Using the getChildByName method
      3m 6s
  5. 56m 20s
    1. Understanding timelines
      4m 15s
    2. Using common timeline navigation methods
      5m 34s
    3. Using the currentFrame and totalFrames properties
      8m 2s
    4. Controlling the timeline of an instance
      6m 41s
    5. Detecting and navigating frame labels with ActionScript
      7m 57s
    6. Solving problems when timelines and ActionScript animation collide
      4m 16s
    7. Condensing a multi-frame timeline into one frame
      8m 33s
    8. Creating a simple slide presentation app in the timeline
      7m 51s
    9. Using one event handler with multiple buttons
      3m 11s
  6. 36m 5s
    1. Creating a class using Flash templates
      4m 43s
    2. Setting a document class
      6m 51s
    3. Preparing a class to be connected to a symbol
      4m 31s
    4. Using the Symbol Properties menu to connect a symbol to a class
      4m 55s
    5. Resolving problems with instances in a linked class
      7m 53s
    6. Understanding packages
      3m 17s
    7. Working with ActionScript source paths
      3m 55s
  7. 44m 32s
    1. Viewing the finished game
    2. Viewing the FLA file
      2m 9s
    3. Creating the DragDrop and Map classes
      2m 51s
    4. Linking the draggable class to Library symbols
      2m 47s
    5. Adding drag-and-drop functionality
      3m 38s
    6. Saving and resetting an object's position
      3m 33s
    7. Giving a target drop object to the draggable objects
      13m 16s
    8. Showing a Win screen
      7m 3s
    9. Resetting the game
      8m 24s
  8. 29m 6s
    1. Loading bitmap images from the Library
      4m 6s
    2. Loading bitmap images from external files
      5m 22s
    3. Adding mouse functionality to bitmap images
      3m 31s
    4. Using a loop to load multiple images
      6m 14s
    5. Creating a simple slideshow
      8m 37s
    6. Using Flash Player 10 color management
      1m 16s
  9. 27m 13s
    1. Loading an external SWF
      4m 14s
    2. Running ActionScript code in an external SWF from its parent
      5m 30s
    3. Running parent code in a child SWF
      5m 7s
    4. Creating a timeline-based preloader to load an external SWF file
      5m 3s
    5. Displaying playback progress of a loaded SWF file
      7m 19s
  10. 40m 10s
    1. Creating plain text files
      2m 8s
    2. Loading text from an external text file
      6m 26s
    3. Loading multiple text files
      6m 43s
    4. Rendering simple HTML in a text field
      5m 51s
    5. Creating a scroll bar for a text field
      5m 29s
    6. Scrolling a text field
      4m 59s
    7. Scrolling movie clips and other objects using masks
      5m 42s
    8. Modifying TLF text properties through ActionScript
      2m 52s
  11. 23m 40s
    1. Reviewing XML and E4X syntax
      3m 29s
    2. Loading an XML file
      3m 26s
    3. Using dot syntax to access XML data
      4m 2s
    4. Using XML data to populate a DataGrid component
      7m 4s
    5. Using XML data to load image files
      5m 39s
  12. 23m 33s
    1. Loading audio from the Library
      1m 41s
    2. Loading audio from external files
      3m 41s
    3. Playing, pausing, and stopping sounds
      5m 39s
    4. Muting all audio with the SoundMixer.stopAll method
      1m 28s
    5. Tracking load progress
      2m 38s
    6. Displaying sound position
      5m 5s
    7. Adjusting volume
      3m 21s
  13. 19m 54s
    1. Touring the FLA file
      5m 57s
    2. Controlling video playing and pausing with ActionScript
      1m 56s
    3. Working with ActionScript cue points to add closed captioning
      3m 35s
    4. Displaying video playback position
      3m 44s
    5. Adjusting video volume
      4m 42s
  14. 5m 32s
    1. Using new code snippets for AIR and mobile
      1m 13s
    2. Viewing the new code snippets HUD
      1m 17s
    3. Loading assets with the new ProLoader class
      1m 8s
    4. Understanding Flash Player premium features
      1m 54s
  15. 16s
    1. Goodbye

please wait ...
Working with conditional statements
Video Duration: 9m 49s 7h 8m Beginner Updated May 23, 2012


Working with conditional statements provides you with in-depth training on Developer. Taught by Todd Perkins as part of the ActionScript 3.0 in Flash Professional CS5 Essential Training

View Course Description

In ActionScript 3.0 in Flash Professional CS5 Essential Training, Todd Perkins shows Flash designers how to incorporate ActionScript code into their projects and create interactive presentations and applications. The course includes a review of ActionScript language basics and the object-oriented programming (OOP) methodology, a tour of those Flash Professional CS5 features designed for developers, such as code hinting and the Code Snippets panel, and instructions on interacting with objects in the Library and placing code on the Timeline. Exercise files are included with the course.

Topics include:
  • 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
Developer Web
ActionScript Flash Professional

Working with conditional statements

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.

Find answers to the most frequently asked questions about ActionScript 3.0 in Flash Professional CS5 Essential Training .

Expand all | Collapse all
please wait ...
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.





Don't show this message again
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.

Mark all as unwatched Cancel


You have completed ActionScript 3.0 in Flash Professional CS5 Essential Training.

Return to your organization's learning portal to continue training, or close this page.


Upgrade to View Courses Offline


With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

Become a Member and Create Custom Playlists

Join today and get unlimited access to the entire library of online learning video courses—and create as many playlists as you like.

Get started

Already a member?

Log in

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

You started this assessment previously and didn’t complete it.

You can pick up where you left off, or start over.

Resume Start over

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from

Sign up and receive emails about and our online training library:

Here’s our privacy policy with more details about how we handle your information.

submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.