Flash MX/ActionScript Graphics Tutorial (12)

Source: Internet
Author: User
Tags mathematical functions
Tutorial 2.4 Movie Clip Events
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, so 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 (typically used to initialize the movie clip) in the following script panel:

Onclipevent (Load)

{

}

The effect of onclipevent (unload) is the opposite of onclipevent (load), which occurs when a movie clip disappears.

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. A falling animation can be performed in such a way that a movie clip is first created as a bomb, dragged and dropped into a scene to create a movie clip instance, let this instance capture the Enterframe event, and design such code for it:

Onclipevent (Enterframe)

{

this._roation+=10;

this._y+=10;

}

The meaning of this code is to rotate the movie clip (this) 10 degrees every other frame, 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 appears to be similar to the many times I've seen before, and these events do occur when the mouse moves, 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 it is 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.

Understanding the MouseMove events, the meaning of MouseDown and MouseUp is not difficult to understand.

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 action

}

}

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 carries out data loading operations, 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.

Many of the events are described earlier, but the fact that action script provides a lot of events. 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.