Turning Power On/Off

Chia sẻ: Qweqwdasd Qweqdasda | Ngày: | Loại File: PDF | Số trang:8

lượt xem

Turning Power On/Off

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Turning Power On / Off Đôi khi hành động nhất định thực hiện chỉ khi một cái gì đó được bật. Một chiếc xe là một ví dụ thực tế: cố gắng lái xe mà không cần bắt đầu từ nó và bạn sẽ không nhận được đến nay. Một chiếc xe được thiết kế để nó sẽ chỉ chạy sau khi bạn đã cho nó sức mạnh bằng cách bật lửa này.

Chủ đề:

Nội dung Text: Turning Power On/Off

  1. < Day Day Up > Turning Power On/Off Sometimes certain actions execute only if something is turned on. A car is a real-world example: try driving the car without starting it and you won't get far. A car is designed so that it will run only after you've given it power by turning on the ignition. In similar fashion, something can happen in your movie to trigger your script's functionality (to 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. For example, consider this script: function clockEnabled(power:Boolean){ if(power == true){ clock_mc.onEnterFrame = function () { this._rotation += 1; } }else{ delete clock_mc.onEnterFrame; } } When this function is called, it's passed a value of true or false, such as:
  2. clockEnabled(true); The conditional statement within the function checks this value and reacts accordingly. If the value true is passed to the function, an onEnterFrame event is attached to the clock_mc movie clip instance, causing it to rotate 1 degree with each onEnterFrame event. As a result, the clock appears to have its power turned on. If you wanted to "unplug" the clock's power, you'd simply call the function again, passing it a value of false: clockEnabled(false); The function checks this value and reacts accordingly. In this case, the conditional statement is set up so that sending the function a value of false causes the onEnterFrame event to be deleted, causing the clock to stop its rotation. The function call essentially becomes the on/off switch for the clock. Many desktop software applications make use of this scripting functionality to allow users to turn program capabilities on and off through preference settings in dialog boxes. Because Flash projects are usually steeped in multimedia and animation—which are often triggered by an executing script—you can use this on/off capability to restrict a script's power until you need it, essentially turning off timelines, animations, and more. The next exercise uses these scripting concepts to put the rocket into motion only after the Launch button has been clicked and released. 1. Open rocketLaunch3.fla. We'll continue building on the file you worked with in the preceding exercise. 2. With the Actions panel open, select Frame 1 of the Actions layer and add this script at the end of the current script: 3. 4. var rocketLaunched:Boolean = false; 5. 6. var speed:Number = noThrust; 7.
  3. The first line creates the rocketLaunched variable and assigns it an initial value of false. This variable will be used to track whether the rocket is in motion (the importance of the rocketLaunched variable is discussed in the next exercise). The initial value is false because the rocket is initially stationary. The purpose of the second line of script is to set the value of a variable named speed to the value of noThrust. The setWeatherConditions() function created in the first exercise of this lesson sets the values of thrust and noThrust based on the value of randomWeather. When this line of script is executed, noThrust will have a value of 1, 2, or 3—that is, the value that speed is set to. (The value of the speed variable will be used to set the intital speed of the rocket.) 3. Add this function definition after the script you just added in Step 2: 4. 5. function rocketLaunch(){ 6. 7. rocketLaunched = true; 8. 9. sounds_mc.gotoAndPlay("launch"); 10. 11. rocket_mc.gotoAndStop("on"); 12. 13. rocket_mc.onEnterFrame = function(){ 14. 15. this._y -= speed; 16. 17. if(this._y < _y){ 18. 19. launchSuccess(); 20. 21. } 22. 23. } 24. 25. } 26.
  4. The rocketLaunch() function sets the rocket into motion. In a moment, we'll set up a Launch button to call this function. First, let's take a look at what happens when this function is called. The function begins by setting the value of rocketLaunched to true, indicating that the rocket has been put into motion. The next line sends the sounds_mc movie clip instance to the frame labeled launch, where an audio clip of a rocket blasting off will play. The next line sends the rocket_mc instance to the frame labeled on, causing flame to appear at the bottom of the rocket. Next, an onEnterFrame event handler is dynamically attached to the rocket_mc instance; the script within the event handler is executed at the frame rate of our movie, 24 frames per second. The first line within the event handler moves the rocket to its current vertical position, minus the value of speed. We've used a scripting shortcut of this._y -= speed to denote the longhand version of
  5. this._y = this._y - speed Remember that the value of speed is set to the value of noThrust, as described in Step 2. If speed is set to 2, with each onEnterFrame event the _y property value of rocket_mc will be decreased by 2, causing it to move upward. NOTE Remember that the use of this in an event handler method is a direct reference to the object to which the event handler method is attached. As such, this._y in this step is a reference to the _y property of the rocket_mc movie clip instance. With the rocket_mc movie clip instance moving upward, its y position will eventually exceed the y position of the stage (the top of the stage). The if statement within the event handler method checks for this occurrence. Because this if statement is within the onEnterFrame event handler, the condition is checked 24 times a second. When it becomes true, the launchSuccess() function is called, turning off power to the rocket. 4. Add the following function definition after the current script: 5. 6. function launchSuccess(){ 7. 8. rocketLaunched = false; 9. 10. status_mc.gotoAndStop("success"); 11. 12. sounds_mc.gotoAndPlay("success"); 13. 14. rocket_mc.gotoAndStop("off"); 15. 16. delete rocket_mc.onEnterFrame; 17. 18. } 19. This function is called when the rocket's y position exceeds that of the stage. It begins by setting the value of the rocketLaunched variable to false, indicating that
  6. the rocket has completed its ascent and is no longer moving. The next line sends the status_mc movie clip instance to the frame labeled success, causing the message "Launch Successful" to appear in the middle of the screen. The next line sends the sounds_mc movie clip instance to the frame labeled success on the clip's timeline. At this label, a short audio clip plays, indicating that the launch was successful. The last line deletes the onEnterFrame event handler method from the rocket_mc movie clip instance. Because the rocket has moved beyond the boundary of the stage, this event handler method, which moves the rocket upward, is no longer necessary. Power to the rocket is turned off. 5. Add this line of script at the end of the current script: 6. 7. launch_btn.onRelease = rocketLaunch; 8. Here we've set our Launch button, named launch_btn, to call the rocketLaunch() function when the button is clicked and released. This action causes the functionality we discussed in Steps 3 and 4 to execute. Let's try it. 6. Choose Control > Test Movie. Click the Launch button and the rocket begins moving upward. As soon as it moves past the top of the stage, any further movement stops and the application
  7. provides visual and auditory indicators showing that the launch was successful. We want the user to be able to launch the rocket more than once, so let's add a script to enable this functionality. 7. Close the test movie to return to the authoring environment. With the Actions panel open, select Frame 1 of the Actions layer and add the following script at the end of the current script: 8. 9. function rocketReset(){ 10. 11. rocketLaunched = false; 12. 13. status_mc.gotoAndStop("off"); 14. 15. sounds_mc.gotoAndPlay("intro"); 16. 17. rocket_mc.gotoAndStop("off"); 18. 19. rocket_mc._x = 98; 20. 21. rocket_mc._y = 352; 22. 23. delete rocket_mc.onEnterFrame; 24. 25. } 26. 27. launch_btn.onPress = rocketReset; 28. The first part of this script defines a function named rocketReset(). The purpose of this function is to reset the elements in the scene to their original states. The function first resets the value of rocketLaunched to false. The three lines that follow reset the status_mc, sounds_mc, and rocket_mc movie clip instances to their initial frame labels. The next two lines place the rocket_mc movie clip instance back on the launch pad. The last line in the function definition removes the onEnterFrame event handler from the rocket_mc movie clip instance, essentially turning it off for the purpose of resetting the scene. The last line sets the Launch button to call this function whenever the button is clicked. 8. Choose Control > Test Movie to test the functionality of the project up to this
  8. point. Click and release the Launch button to set the rocket into motion. Holding down the Launch button at this point will reset the scene. Releasing it again restarts the launch process. 9. Close the test environment to return to the authoring environment, and save your work as rocketLaunch4.fla. We'll build on this file in the next exercise. < Day Day Up >
Đồng bộ tài khoản