Many Flash applications, especially games, are able to detect object collisions one of the most easily understood applications of conditional logic. To detect collisions between two objects (movie clip instances), ActionScript provides the hitTest() method of the Movie Clip object. Using this method in conjunction with a conditional statement allows you to take action when two movie clips collide. You simply define in the script the various actions you want to take, depending on which object is hit. Take a look at the following script: onClipEvent (enterFrame) { if (hitTest ("wallOfCotton")) { pain = 0; } else if (hitTest ("wallOfCardboard")) { pain = 5; } else if (hitTest ("wallOfBricks")) { pain = 10; } } If the movie clip instance to which this script is attached collides with the movie clip instance named wallOfCotton, pain is set to 0. If it collides with wallOfCardboard, pain is set to 5. And finally, if it collides with wallOfBricks, pain is set to 10. As you can see, the hitTest() method is very straightforward. In this exercise, we'll use the hitTest() method in conjunction with a conditional statement to take specific action if the rocket movie clip instance collides with either of the two red bars (our launch window) that are in motion. If the instance hits either one of them, the launch will abort; if it passes through the space between them, the launch will be successful. -
Open rocketLaunch5.fla in the Lesson9/Assets folder. This file continues on the one we were working with in the last exercise. -
Double-click one of the two red bars at the top of the stage to edit this clip in place. Obviously, both red bars are part of this movie clip instance. However, each of the red bars is also a movie clip instance itself, with one flipped horizontally in the opposite direction of the other. Because each of these bars is a movie clip instance, you can use them to react to a collision with the rocket movie clip instance. -
With the Actions panel open, select one of the red-bar instances and add the following script: onClipEvent (enterFrame) { if (hitTest("_root.rocket")) { _root.rocket._x = 98; _root.rocket._y = 352; _root.rocket.launch = false; _root.rocket.gotoAndStop ("off"); _root.status.gotoAndStop ("abort"); _root.sounds.gotoAndPlay ("abort"); } } This script is executed using the enterFrame event, which means this conditional statement is analyzed 24 times a second. The condition that is being looked for is whether the rocket movie clip instance (root.rocket ) has collided with this instance. Whenever it does, the actions in the statement are executed. The first two reset the rocket to its original position on the stage. The next action sets the value of launch on the rocket instance's timeline to false , causing it to cease moving upward. The next action also moves that instance's timeline to the frame labeled off. At this label the flame does not appear under the rocket. Next the status movie clip instance is moved to the frame labeled abort. This will display a message in the middle of the screen stating that the mission has been aborted. Lastly, the sounds movie clip instance is moved to the frame labeled abort, where a short audio clip will play, indicating that the launch has been aborted. -
With the Actions panel open, select the other red bar and place the above script on it as well. If the rocket instance collides with this instance, the actions described in the previous step will occur. -
Choose Control > Test Movie to test your project up to this point. Your project is now fully functional! Press and hold the Launch button to experience what happens at prelaunch. Release it to launch the rocket. Apply thrusters with the idea of getting the rocket between the space in between the red bars. If you do, the launch will be successful; if you don't, it will abort. -
Close the test environment to return to the authoring environment and save your work as rocketLaunch6.fla. This completes the lesson. By now, you have seen that by creating scripts that use conditional logic, the dynamism of your project can increase several-fold, providing a unique experience to every person that views and interacts with it. |