REACTING TO MULTIPLE CONDITIONS


For a successful rocket launch to occur, a number of conditions must be met. One of the most important variables affecting a rocket launch is weather, which helps determine not only how the rocket is propelled but how it's controlled as well. In this exercise, we'll begin scripting our project so that the rocket will react differently based on randomly generated weather conditions.

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

    This file contains a single scene made up of seven layers, named according to their content. Let's take a quick look at the various elements in the scene.

    The Weather layer contains a movie clip instance the size of the movie itself. This movie clip represents the background sky in the scene. Its timeline contains three frame labels: Sunny, Rainy, and Night. At each of these frame labels, the sky graphic changes to match the name of the label. At Sunny, the sky appears sunny; at Rainy, it appears overcast, and at Night, it looks dark.

    The Ground/Central Graphics layer contains the mountains, ground, and main Central Command graphics. There is also a small movie clip instance of a red dot, which blinks to indicate the launch location on the globe.

    The Rocket layer obviously contains our rocket that is, a movie clip instance named rocket. Its timeline contains two frame labels: off and on. At the off label, the rocket doesn't show a flame; at the on label, it does. This provides a visual indicator of when the rocket is in motion.

    The Control Panel layer contains several elements, the most conspicuous of which is the red Launch button. This will be used not only to launch the rocket but also to reset the scene after a launch has been attempted. Below this button is a movie clip instance named thrustBoost that includes the text "Thrusters." This clip also contains two frame labels: on and off. At the on label, the text appears big and red to provide a visual indication that thrust is being applied to the rocket. This layer's last element is a text field in the lower-right portion of the stage with an instance name of weatherText. It will provide a textual indication of current weather conditions.

    The Status layer contains a movie clip instance named status that appears as a small circle in the middle of the sky. It appears this way because the first frame of its timeline contains no graphical content. There are three frame labels on this movie clip's timeline: off, abort, and success. At the off frame label, the clip appears as it does now. At the abort label, a text message appears that reads, "Launch Aborted." At the success label, a text message appears that reads, "Launch Successful." This clip will provide a text message about the launch's success or failure.

    The Guides layer contains a movie clip instance of two red bars. The space between the red bars represents the launch window the area the rocket must pass through to have a successful launch.

    The Sound Clips layer contains a movie clip instance named sounds that also appears as a small circle just above the stage, on the left side. Its timeline contains frame labels named intro, launch, abort, and success. At each of these labels is a sound clip that's representative of the frame label where it exists (for example, at the launch label is a launch sound, at the abort label is an abort sound, and so on). Various scripts will send this clip to these frame labels to provide audio effects for our project.

    graphics/09fig05.gif

    Let's begin scripting the project.

  2. With the Actions panel open, select the weather movie clip instance and add the following script:

     onClipEvent (load) {    randomWeather = random (3);    if (randomWeather == 0) {      conditions = "Sunny";      _root.rocket.noThrust = 3;      _root.rocket.thrust = 6;    } else if (randomWeather == 1) {      conditions = "Rainy";      _root.rocket.noThrust = 2;      _root.rocket.thrust = 4;    } else {      conditions = "Night";      _root.rocket.noThrust = 1;      _root.rocket.thrust = 2;    }  } 

    This script is executed as soon as this movie clip instance loads. Its first action sets the value of the randomWeather variable, based on a randomly generated number (three possibilities of 0, 1 or 2). Next, an if statement is used to analyze the value of randomWeather so that the values of three other variables can be set. If randomWeather has a value of 0, the variable named conditions is created on this timeline and assigned a string value of "Sunny." In addition, two variables are created on the rocket movie clip instance's timeline: one named noThrust and the other thrust , with values of 3 and 6, respectively. If randomWeather does not have a value of 0, then that part of the statement is ignored, at which point an else if statement checks to see if randomWeather has a value of 1. If it does, the same variables we just discussed are instead assigned values of "Rainy," 2, and 4, respectively. If randomWeather doesn't have a value of 1 either, that part of the statement is ignored as well, at which point the actions under the else statement are executed. Remember that an Else statement enables you to define what happens if none of the previous conditions in the statement have proven true but you still want something to happen. In this case, if the previous conditions proved false , the actions under the else statement are executed, setting the value of the variables we've been discussing to "Night," 1, and 2, respectively.

    The value of the conditions variable will be used in a moment to set how the weather will appear graphically in the scene. The other two variables will soon be used to establish how fast the rocket instance will move based on the weather condition generated.

  3. With the Actions panel open, add the following script inside the load event, just below the last part of the if statement:

     gotoAndStop (conditions);  _root.weatherText.text = "Weather: " + conditions; 

    These two actions occur as soon as the if statement is analyzed, and both use the value of conditions to react in unique ways. The first action will move this movie clip instance to a frame label based on the value of conditions . If conditions has a value of "Rainy", this movie clip instance will move to that frame label, where the weather will appear rainy. The next action determines what will be displayed in the weatherText text field in the bottom-right portion of the stage. Once again, if conditions has a value of "Rainy", this text field will display "Weather: Rainy".

    graphics/09fig06.gif

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

    As soon as the movie plays, the weather movie clip is loaded and the script we just added is executed. Depending on the random number generated in the script, the graphical representation of the weather is affected as well as the text displayed in the bottom-right portion of the screen.

    NOTE

    Closing the test movie and then choosing Control > Test Movie again should give you different results because a different random number will be generated, causing the scene to react and appear accordingly.

  5. While still in the testing environment, choose Debug > List Variables.

    This opens the Output window, allowing you to see the value of variables on different timelines. A quick glance will reveal the values of the thrust and noThrust variables in the rocket timeline. We will use these values shortly.

    graphics/09fig07.gif

  6. Close the test environment to return to the authoring environment and save your work as rocketLaunch2.fla.

    We will 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