Flash MX 2004 Programming (AS2.0) tutorial (12)

Source: Internet
Author: User
Tags functions mathematical functions
Programming | Tutorials 2.4 Movie Clip event


the movie clip events are slightly more difficult to understand than the mouse events, keyboard events, and frame events described earlier. When we put a movie clip in the scene, he becomes an "event emitter", constantly reporting his or her running state, if we capture such an event, we can react and deal with it accordingly. There are a variety of film clips, which are described in categories below.





2.4.1onClipEvent (Load) and onclipevent (unload)


This event occurs when a movie clip is loaded, what is the load of the movie clip? For example, if you add a movie clip clip_mc to the scene and let him appear in the movie at frame 10th, it triggers the event at frame 10th. To handle this event, you can select a movie clip clip_mc, and then enter such code in the script panel below (which is typically used to initialize the movie clip):





onclipevent (Load)





  {





  }




The effect of
onclipevent (unload) is the opposite of what onclipevent (load) does, when the movie clip disappears, this event occurs.





2.4.2OnClipEvent (enterframe)

The
Enterframe event continues to occur as the movie plays, and the movie clips trigger a Enterframe event every time the movie progresses. Assuming that the playback rate for the movie is 20 frames per second, the event is generated at 20 times per second. You will certainly ask, what is the effect of such an event? In fact, in the movie clip event, Enterframe should say is most commonly used, because he can use to produce quite complex animation. Let's say we're going to make a bomb. The animation that falls from the air can follow this step: first create a movie clip as a bomb, drag it into a scene to create a movie clip instance, let this instance capture the Enterframe event, and design This code for it:





Onclipevent (Enterframe)





  {





this._roation+=10;





this._y+=10;





  }





This code means that every other frame rotates the movie clip (this) 10 degrees, while moving 10 units along the vertical. A "+ +" operator is used here, which can be a bit confusing, but it's a commonly used operator shorthand, meaning "add yourself a value", such as this._rotation+=10 and This._rotation=this._. The rotation+10 is equivalent.





2.4.3onClipEvent (Mounsemove), Onclipevent (MouseDown) and Onclipevent (mouseUp)


This event seems somewhat similar to the many times seen before, and these events do occur when the mouse is moving, but pay special attention to the fact that regardless of where the mouse is in the scene (not necessarily above the movie clip), this event occurs. Every time the mouse moves, the MouseMove event occurs once. Again, you have to ask, what can this incident do? For example, we need to make a game like a bubble dragon, the bottom of the screen there is a cannon, when the mouse is moving, the muzzle will always point to the direction of the mouse. This effect can be achieved onclipevent (MouseMove). Specifically, the cannon is made into a movie clip, and then the code is added to it:





onclipevent (mouseMove)





  {





var_angle= ((this._x-_root._xmouse)/(This._y-_root._ymouse));





Trace (Var_angle);





Var_angle=math.atan (Var_angle);





Var_angle=var_angle*180/math.pi;





This._rotation=-var_angle;





  }





This code uses several of the mathematical functions provided by Flash, which are inside the math object, so they are very important to use. First, the horizontal and vertical coordinates of the current movie clip are subtracted from the horizontal and vertical coordinates of the mouse. Then the sine of the angle is computed and the value is converted to an angle (Math.atan) using the inverse tangent function, noting that the result is a radian number, which needs to be converted into a common angle, and finally copied to the movie clip. Rotation parameters, to achieve the cannon with the mouse pointer movement and rotation effect.





understand the MouseMove event, MouseDown and MouseUp meaning is not difficult to comprehend.





2.4.4OnClipEvent (KeyDown) and Onclipevent (KeyUp)


These two events are similar to the previous on (Keypress) and are used to capture keystroke events on the keyboard. However, it is particularly emphasized that the functions of the onclipevent (KeyDown) and Onclipevent (KeyUp) are much more powerful, combining them with the ability to capture key combinations (such as CTRL + a) that are pressed by the user, This is much more powerful than the ability to only capture an on (Keypress) of a single button action. For example, if we need to capture the user's ctrl+f action, we can use this code:





onclipevent (keyUp)





  {





if (Key.isdown (Key.control) && Key.isdown (70))





  {





//Perform the appropriate operation





  }





  }





This code uses the built-in object key, which is useful for handling keystrokes. The Isdown () method is used to monitor whether a key has been pressed, and the parameter of this function can be a key value such as 70 corresponding to the F key. You can also use a key name that is built into it, such as Key.control, which refers to the CTRL key. You can tell whether the CTRL and F keys are pressed at the same time by logical and operational results (&&) If the two keys are pressed.





2.4.5OnClipEvent (data)


If a movie clip instance has a data load operation, then the Onclipeevent (data) event occurs when the load completes. For example, we added a code execution variable load operation to the on (press) event handling of a button and set the target parameter of the code load function loadvariables () to a movie clip. Then add the onclipevent (data) event to the movie clip, and the movie clip will receive a onclipevent (data) event after the argument is loaded.





describes many of the events earlier, but in fact there are many events that the action script provides. In addition to the very intuitive events of keyboard events, mouse events, and so on, there are some more "abstract" events. For example, an event occurs when a sound is finished playing, and an event occurs when the user adjusts the viewer's window for a large hour. Capturing and handling these events can further enhance the interactive performance of flash movies.








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.