
Detecting Collisions
Thông tin tài liệu:
Nội dung trích xuất từ tài liệu:
Detecting Collisions < Day Day Up >Detecting CollisionsMany Flash applications, especially games, are able to detect object collisions—one ofthe most easily understood applications of conditional logic.To detect collisions between two objects (movie clip instances), ActionScript providesthe hitTest() method of the MovieClip class. Using this method in conjunction with aconditional statement allows you to take action when two movie clips collide. You definein the script the various actions you want to take, depending on which object is hit. Takea look at the following script:myBody_mc.onEnterFrame = function() { if (hitTest (wallOfCotton_mc)) { pain = 0; } else if (hitTest (wallOfCardboard_mc)) { pain = 5; } else if (hitTest (wallOfBricks_mc)) { pain = 10; }}If the movie clip instance to which this event handler is attached (myBody_mc) collideswith the movie clip instance named wallOfCotton_mc, pain is set to 0. If myBody_mccollides with wallOfCardboard_mc, pain is set to 5. Finally, if myBody_mc collides withwallOfBricks_mc, pain is set to 10. The hitTest() method is very straightforward.In the following exercise, we use the hitTest() method in conjunction with a conditionalstatement to take specific action if the rocket_mc movie clip instance collides with eitherof the two red bars (the launch window) that are in motion. If the rocket hits either bar,the launch aborts; if the rocket passes through the space between the bars, the launch issuccessful. 1. Open rocketLaunch5.fla. This step builds on the file you worked with in the preceding exercise. 2. Double-click launchWindow_mc at the top of the stage to edit this clip in place. This movie clip instance contains other two movie clip instances: the red bars, with one flipped horizontally in the opposite direction of the other. The red bar on the left is named leftGuide_mc and the one on the right is named rightGuide_mc. Because both of these bars are movie clip instances, you can use them to react to a collision with the rocket_mc movie clip instance. 3. Return to the main timeline. With the Actions panel open, select Frame 1 of the Actions layer and add the following script: 4. 5. 6. launchWindow_mc.onEnterFrame = function () { 7. 8. if (this.leftGuide_mc.hitTest (_root.rocket_mc) || this.rightGuide_mc.hitTest (_root 9. 10. .rocket_mc)) { 11. 12. launchAbort (); 13. 14. } 15. 16. }; 17. The script attaches an onEnterFrame event handler to the launchWindow_mc movie clip instance. The script executes at our projects frame rate of 24 frames a second. The conditional statement within this script does one thing: using the hitTest() method and the OR logical operator (||), it checks whether the rocket_mc movie clip instance has collided with the leftGuide_mc or rightGuide_mc movie clip instance, which are both inside the launchWindow_mc movie clip instance. If at any time either condition proves true, the launchAbort() function is called. Lets create that function, and wrap up our work on this project.4. Place this function definition at the end of the current script:5.6. function launchAbort () {7.8. rocketLaunched = false;9.10. status_mc.gotoAndStop (abort);11.12. sounds_mc.gotoAndPlay (abort);13.14. rocket_mc.gotoAndStop (off);15.16. rocket_mc._x = 98;17.18. rocket_mc._y = 352;19.20. delete rocket_mc.onEnterFrame;21.22. }23. This function works in almost the same manner as that of the rocketReset() function we created earlier in this lesson. The difference is that both the status_mc and sounds_mc movie clip instances are moved to the frame labeled abort in the launchAbort() function. Scripting for our project is complete. Lets do a final test.5. Choose Control > Test Movie to test your project. Click and hold down the Launch button to experience what happens at prelaunch. Release the button to launch the rocket. Apply thrusters with the idea of getting the rocket through the space between the red bars. If you get through, the launch will be successful; if you dont, it will abort.6. Close the test environment to return to the authoring environment, and save your work as rocketLaunch6.fla. This step completes the exercise and this lesson. By using conditional logic in your scripts, you can greatly increase your projects dynamism and provide a unique experience to every person who interacts with it. < Day Day Up >
Tìm kiếm theo từ khóa liên quan:
máy tính mạng máy tính internet phần mềm ứng dụng lập trình SQL HTML sever web XMLTài liệu có liên quan:
-
Giáo án Tin học lớp 9 (Trọn bộ cả năm)
149 trang 295 0 0 -
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 2
102 trang 295 0 0 -
Ngân hàng câu hỏi trắc nghiệm môn mạng máy tính
99 trang 278 1 0 -
Bài giảng: Lịch sử phát triển hệ thống mạng
118 trang 278 0 0 -
47 trang 250 4 0
-
Đề cương chi tiết học phần Thiết kế và cài đặt mạng
3 trang 246 0 0 -
80 trang 238 0 0
-
6 trang 227 0 0
-
Giáo trình môn học/mô đun: Mạng máy tính (Ngành/nghề: Quản trị mạng máy tính) - Phần 1
68 trang 225 0 0 -
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 1
122 trang 223 0 0 -
122 trang 222 0 0
-
Giáo trình môn học Mạng máy tính - Nghề: Quản trị mạng máy tính - Trình độ: Cao đẳng nghề (Phần 2)
39 trang 215 0 0 -
Giáo trình căn bản về mạng máy tính -Lê Đình Danh 2
23 trang 214 0 0 -
6 trang 210 0 0
-
Bài giảng Lý thuyết mạng máy tính: Chương 3 - ThS. Lương Minh Huấn
73 trang 197 0 0 -
Đề cương chi tiết học phần: Mạng máy tính và lập trình mạng
4 trang 192 0 0 -
Chuyên đề mạng máy tính: Tìm hiểu và Cài đặt Group Policy trên windows sever 2008
18 trang 190 0 0 -
139 trang 181 0 0
-
Giáo trình Mạng máy tính (Nghề: Tin học ứng dụng - Trung cấp) - Trường Cao đẳng Cộng đồng Đồng Tháp
189 trang 174 0 0 -
Giáo trình Ứng dụng công nghiệp thông tin cơ bản: Phần 1
73 trang 172 0 0