Reacting to Multiple Conditions


The best way to understand conditional logic is to examine it in the context of a simple application. We will use some of the concepts we've learned to script a project that reacts to user interaction.

In our project we will have a MovieClip in the center of the stage that will move based on the user pressing the arrow keys on the keyboard. It's a very simple idea that will clearly demonstrate conditional logic.

1.

Open Key01.fla in the Lesson03/Start folder.

This FLA file contains a single frame with two layers, named according to their contents. Let's look at the various elements in the scene.

The Target layer contains one MovieClip placed on the center of the Stage with an instance name of target. This is the clip we will be moving as the user presses the arrow keys.

The Actions layer will contain all the scripts for this project. Let's begin.

2.

With the Actions panel open, select Frame 1 of the Actions layer and add the following script:

    this.onKeyDown = function() {       if(Key.getCode() == Key.RIGHT) {         trace("Move Right");       }else if(Key.getCode() == Key.LEFT) {         trace("Move Left");       }else if(Key.getCode() == Key.UP) {         trace("Move Up");       }else if(Key.getCode() == Key.DOWN) {         trace("Move Down");       }     }     Key.addListener(this);


In this code block we have created an onKeyDown event handler for the Key class. We started by creating a function called onKeyDown. This function has an if/else if statement that checks for the key that has been pressed. To add this handler as a listener to the Key class we need to call the addListener method after we have defined the handler function. We're using the TRace statement in this example to send a message to our output panel. This will help us determine which case is being caught.

Hopefully, looking at this conditional logic has given you an idea of how this could be done a little better. This is the perfect situation to use a switch/case statement. Let's go ahead and revise our event handler to use a switch statement now.

3.

Replace the onKeyDown handler with this script; this is represented in Key02.fla in the Lesson03/Complete folder:

  this.onKeyDown = function() {     switch(Key.getCode()) {       case Key.LEFT:         trace("Move Left");         break;       case Key.RIGHT:         trace("Move Right");         break;       case Key.UP:         trace("Move Up");         break;       case Key.DOWN:         trace("Move Down");         break;     }   }


As you can see from this code, a switch statement is much cleaner and easier to read. It's also more efficient because it will only evaluate the Key.getCode method once, and then switch based on its result.

4.

Close and save your work as Key02.fla.




Macromedia Flash 8 ActionScript Training from the Source
Macromedia Flash 8 ActionScript: Training from the Source
ISBN: 0321336194
EAN: 2147483647
Year: 2007
Pages: 221

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net