Sometimes certain actions should be executed only if something is turned on. A real-world example of this would be a car: Try driving one without starting it, and chances are, you won't get far. That car is designed so that it will only run after you've given it power by turning the ignition. In similar fashion, something can happen in your movie to trigger your script's functionality (turn it on).

Think of it this way: Events power scripts by triggering actions. The scripts themselves represent the mechanism, or "machine," for accomplishing various tasks. Conditional logic can be used as an On/Off switch in your scripts, allowing actions to execute, as the result of an event, only under certain circumstances.

Take, for example, the following script:

 onClipEvent (enterFrame) {    clock._rotation = clock._rotation + 1;  } 

With this script, a movie clip instance named clock is rotated by 1 degree with each enterFrame event regardless of what else is happening in your movie. If you wanted to "unplug" your clock so that it would only rotate if it had "power" you could revise the preceding script to read as follows:

 onClipEvent (enterFrame) {    if (power == true) {  clock._rotation = clock._rotation + 1;    }  } 

With this script, the clock only rotates if the variable power has a value of true . You can set the value of this variable to false or back to true again by using a script attached to a button, frame, or other movie clip instance in essence, creating an On/Off switch for the script.

Many desktop software applications make use of this scripting functionality to allow users to turn on and off program capabilities through preference settings dialog boxes. Because Flash projects are usually steeped in multimedia and animation which are often triggered by an executing script you can use this capability to restrict a script's power until you need it, essentially turning off timelines, animations and more.

In the following exercise, we'll use the above scripting concepts to put our rocket in motion only after the Launch button has been pressed and released.

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

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

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

     onClipEvent (load) {    speed = noThrust;  } 

    This script is executed as soon as the movie clip loads. Its only purpose is to set the value of a variable named speed to the value of noThrust . You'll remember the script on the weather movie clip instance that sets the values of thrust and noThrust based on the value of randomWeather (this was the first exercise in this lesson, in case you need to review). Thus, when this script is executed, noThrust will have a value of 1, 2, or 3, which is the value that speed is set to. The value of the speed variable will be used in a moment to set the speed at which the rocket will move initially.

  3. Add the following script below the script you just added:

     onClipEvent (enterFrame) {    if (launch) {      _y = _y   speed;      if (_y < _root._y) {        launch = false;        gotoAndStop ("off");        _root.status.gotoAndStop ("success");        _root.sounds.gotoAndPlay ("success");      }    }  } 


    This script is executed (24 times a second) using the enterFrame event (24 times a second). The first part of it says that if launch is true , move the rocket to its current position minus the value of speed . This causes the instance to move upward only if launch is true . If launch is false , nothing happens. We will shortly script the Launch button to set the value of launch to true when it is released, thus setting this instance (rocket) in motion. As mentioned in the last step, the value of speed dictates the rate at which the rocket moves upward.


    In the above statement, using if (launch) is the same as using if (launch == true) . The former is simply a shortcut. Likewise, if we wanted to check whether launch had a value of false , we could simply use if (!launch) .

    Just below the line of script that moves the instance upward, you'll see another if statement notable for its right indentation: This indicates that it, too, is only analyzed when launch is true , just like the line of script above it. This second if statement is said to be nested inside the first. The reason for this is that it checks to see if this instance's vertical position exceeds the top of the stage which is guaranteed to happen eventually because the instance is in upward vertical motion when launch is true . However, because the condition will only exist if launch has been set to true and the instance has been set in motion, it's only necessary to check for it under those circumstances which is why that if statement is nested. If the statement weren't nested, it would be constantly analyzed whether or not the rocket were in motion (launch were true or false ) which would be a waste of processor power.

    Using this nested statement, once the boundary (the top of the stage) has been exceeded, four actions are executed: The first sets the value of launch to false in essence, turning off this script. (In other words, the action gives the script the ability to turn itself off.) The next action sends this instance to the frame labeled off the point on this movie clip's timeline where the flame does not appear under the rocket. The next action sends the status movie clip instance to the frame labeled success, causing the message "Launch Successful" to appear in the middle of the screen. The last action sends the sounds movie clip instance to a frame labeled success. At this label, a short audio clip plays, indicating that the launch was successful.


    As already mentioned, this script depends on the value of launch being true to do anything. We'll next script the Launch button that sets this value.

  4. With the Actions panel open, select the Launch button and add this script:

     on (release) {    rocket.launch = true;    rocket.gotoAndStop ("on");    sounds.gotoAndPlay ("launch");  } 

    Using this script, when this button is released, three actions are executed: The first action sets the value of the launch variable on the rocket movie clip instance's timeline to true (which in turn sets the rocket in motion, as discussed in the previous step). The next action sends the rocket instance to the frame labeled on, causing the flame to appear at the bottom of the rocket. The last action sends the sounds instance to the frame labeled launch, where an audio clip of a rocket blasting off begins to play.

  5. Add this script to the end of the last script:

     on (press) {    rocket.launch = false;    rocket.gotoAndStop ("off");    status.gotoAndStop ("off");    sounds.gotoAndPlay ("intro");    rocket._x = 98;    rocket._y = 352;  } 

    We want this button to take several actions when pressed all of which deal with resetting elements and variables in the scene so that a rocket launch can be attempted more than once. The first action sets the value of launch on the rocket instance's timeline to false to halt the rocket's upward motion. The next three actions move the rocket, status, and sounds movie clip instances to frame labels that return them to their initial states. The last two actions place the rocket instance back on the launch pad.

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

    Press and release the Launch button. As soon as the button is released, the value of launch is set to true . This "turns on" the script on the rocket instance, causing it to move upwards. Once it goes beyond the top of the stage, the script is turned off and several other actions occur, indicating a successful launch. Pressing and holding the Launch button at this point will reset the scene. Releasing it again restarts the process.

  7. Close the test environment to return to the authoring environment and save your work as rocketLaunch4.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