Danh mục

Understanding Event Handler Methods

Số trang: 2      Loại file: pdf      Dung lượng: 13.22 KB      Lượt xem: 8      Lượt tải: 0    
Thư viện của tui

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Hiểu biết về phương pháp tổ chức sự kiện handler Mặc dù chúng tôi đã thảo luận một số sự kiện như vậy, đến nay, chúng tôi đã thực sự chỉ bị trầy xước bề mặt của các phản ứng Flash. Bạn đã biết rằng kịch bản phản ứng với tất cả các loại gây ra: sự tương tác với con chuột, của thời gian đạt đến một khung cụ thể
Nội dung trích xuất từ tài liệu:
Understanding Event Handler Methods < Day Day Up >Understanding Event Handler MethodsAlthough weve discussed a number of events thus far, weve really just scratched thesurface of Flash reactions. You already know that scripts react to all kinds of triggers:interaction with the mouse, the timelines reaching a specific frame, movie clip instancesentering a scene. But did you know that by using event handler methods, you can makeyour scripts execute when sounds finish playing, when the stage is resized, or when thetext in a text field changes? You can even use event handler methods to extend thefunctionality of the events weve already used in this lesson.Although event handler methods and standard event handlers are used for the same basicpurpose (that is, executing a script when something happens in your movie), you mustimplement them a bit differently.By now, you know how to set up a script to be executed as the result of an event. Forexample, this script is attached to a movie clip instance named myMovieClip_mc and isexecuted whenever the mouse button is pressed while the instance is present in the scene:onClipEvent(mouseDown) { this._rotation = 45;}This script will rotate the movie clip instance by 45 degrees when the mouse button ispressed.Using an event handler method, the following script would be placed on a frame of thetimeline to accomplish the same purpose, rotating myMovieClip_mc whenever the mousebutton is pressed:myMovieClip_mc.onMouseDown = function() { this._rotation = 45;}Instead of using onClipEvent to define the event handler (as shown in the first script),here we use a dot (.) to separate the name of the object (in this case myMovieClip_mc)from the event to which it needs to react. And to reiterate, weve placed this script on aframe rather than attached it to the instance.Dont worry about the use of function() in the script. Well provide an in-depth discussionof functions in Lesson 5, Using Functions. All you need to know about this use offunction() is that its a necessary part of the syntax for implementing the event handlermethod.NOTETo execute a particular function when an event handler method is defined, change thesyntax to myMovieClip.onMouseDown = nameOfFunction;.The actions in the second line of the script (between the curly braces) define what needsto happen when the event occurs.Because this script describes how the myMovieClip_mc instance reacts to theonMouseDown event, that instance must exist in the scene at the time the event handlermethod is defined. This will attach the defined functionality to the instance. By the sametoken, event handler methods assigned to objects are removed when the object leaves thescene (or is otherwise removed). If the object appears in the scene again, any eventhandler methods will need to be defined again.At first glance, you may be wondering how event handler methods are much differentfrom regular events, and if there are any advantages of using one over the other. Thatswhat well discuss next.NOTEEvent handler methods play a large role in the way custom objects are set up to react toevents. For more information, see Lesson 7, Creating Custom Classes. < Day Day Up >

Tài liệu được xem nhiều: