REACTING TO USER INTERACTION


Users will interact with your movie in two primary ways via mouse or keyboard, either of which can be used any number of ways. Users can interact with your movie by moving the mouse around the screen, pressing and releasing buttons, and so forth. Using conditional logic, you can script your movie to react in various ways based on the user's interaction with it.

In the following exercise, we'll add functionality to our project that allows the user to press the Space bar on the keyboard to apply "thrusters" to move the rocket upward at a quicker-than-normal pace.

  1. Open rocketLaunch4.fla in the Lesson09/Assets folder.

    This file continues on the one we worked with in the previous exercise.

    There are a couple of important things to keep in mind as you begin working on this exercise: The first thing to remember is that when the weather movie clip instance is loaded, a script is executed that does several things based on the value of a variable named randomWeather . Not only does it display a random weather graphic, it also sets the values of the variables thrust and noThrust . The variable thrust is given a value double that of noThrust at the time they are set. Thus, if noThrust has a value of 2, thrust has a value of 4. The second thing to remember is that when the rocket instance loads, the speed variable on its timeline is set to the value of noThrust ; thus, if noThrust has a value of 2, that's what speed is set to. You'll remember that the value of speed is used to determine how fast the rocket moves upward. In this exercise, we'll set the value of speed to either thrust or noThrust , depending on whether the

    Space bar is pressed down. Because thrust has a value twice that of noThrust , this will propel the rocket upward twice as fast when the Space bar is pressed.

    graphics/09fig12.gif

  2. With the Actions panel open, select the rocket movie clip instance and add the following script just below the last script that appears:

     onClipEvent (keyDown) {    if (launch && key.isDown(key.SPACE)) {    speed = thrust;    _root.thrustBoost.gotoAndStop ("on");    }  } 

    This script is executed whenever a key is pressed at which point a conditional statement looks for two conditions. If launch has a value of true and if the Space bar is pressed down, two actions are executed. The first action sets the value of speed to the value of thrust to move the rocket upward at twice its current rate. The next action tells the thrustBoost movie clip instance (just below the Launch button in the scene) to go to the frame labeled on where big, red text that reads 'Thrusters' is displayed.

    Obviously, we want a mechanism that allows the rocket to return to its initial speed; that's what the next script does.

  3. Add the following script just below the script we added in the last step:

     onClipEvent (keyUp) {    if (!key.isDown(key.SPACE)) {    speed = noThrust;    _root.thrustBoost.gotoAndStop ("off");    }  } 

    graphics/09fig13.gif

    This script is similar in syntax to the last one we added; however, it works in just the opposite way. For starters, it's executed whenever a key is released on the keyboard. A conditional statement then checks to see if the space bar is not pressed down. If it's not, two actions are executed. The first sets the value of speed back to the value of noThrust (its initial value). The next tells the thrustBoost movie clip instance to go to the frame labeled off where the "Thrusters" text appears as it did initially.

  4. Choose Control > Test Movie to test the functionality of your project up to this point.

    Press and release the Launch button. As soon as the button is released, the rocket is set in motion. Pressing the space bar should cause the rocket to move upward at twice its current speed. Releasing the space bar should return the rocket's speed to its initial value.

  5. Close the test environment to return to the authoring environment and save your work as rocketLaunch5.fla.

    We'll build on this file in the next exercise.



Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161

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