Flash MX/ActionScript Graphics tutorial (ix)

Source: Internet
Author: User
Tags implement modify
Tutorial 2.2 Examples
A few of the most commonly used mouse events are outlined earlier, and we'll use a complete example to synthesize these events.
First set up a scene to add three movie clip instances to it, named Hand_mc, Message_txt, eventtrapper_btn, and DRAGTEST_MC,HAND_MC to replace the mouse, Message_txt is a dynamic text, Displays feedback, eventtrapper_btn is a button instance that captures various mouse events, and DRAGTEST_MC is used to demonstrate the handling of drag events.
2.2.1 Custom Mouse
We often need to use a custom mouse pointer in a flash program, and in this case we'll replace the mouse pointer with a custom shape. Go to the main scene, select the first frame, and enter the following code in the script panel:
Stop ();
Mouse.hide ();
StartDrag ("Hand_mc", true);
Message_txt.text = "Start mouse event test";
The first code is Stop (), which is to let the movie play here and stop while waiting for the user's action, mouse.hide () hides the mouse pointer, and then STARTDRAG begins the drag operation on the movie clip instance HAND_MC, because the previous mouse pointer is hidden, So this command now acts as if you were to replace the mouse pointer with an icon. Note that there are two parameters behind the StartDrag, and the first effect is obviously to hand_mc the movie clip instance as a drag object, and the following ture locks the center of the movie clip Instance and the center of the mouse pointer, if set to false, The center of the movie clip will be locked with the point where the mouse is clicked for the first time in the scene. In this example, we use the StartDrag command to simulate the effect of mouse pointer substitution, so it is clear that this argument should be set to true.
2.2.2 Capture and Process events
Now select the movie clip instance eventtrapper_btn and go to the script panel where we can write a series of event-handling code. We introduced a number of mouse-related events, here we will capture each one, pay attention to observe the similarities and differences between them.
On (rollover) {
Message_txt.text = "mouse Floating event";
}
On (RollOut) {
Message_txt.text = "Mouse Out event";
}
On (Press) {
Message_txt.text = "mouse click event";
}
On (dragout) {
Message_txt.text = "The mouse presses the left key on the current object and drags out";
}
On (release) {
Message_txt.text = "Mouse Release event";
}
These are relatively simple events, and we'll deal with a slightly more complex event below.
Select the movie clip instance DRAGTEST_MC, and then enter such code in the Code panel
On (dragOver) {
This._alpha = this._alpha-10;
}
The code inside this event is executed when the user presses the left mouse button and drags on the DRAGTEST_MC. This variable has been dealt with before, its function is to refer to the current object (that is, DRAGTEST_MC), _alpha is one of its attributes, transparency, where we use a simple operation to reduce its transparency, the final effect is similar to the image is erased by the eraser.
Then select the button eventtrapper_btn to add the code for it:
On (releaseoutside) {
eventtrapper_btn._x = _root._xmouse;
eventtrapper_btn._y = _root._ymouse;
}
This code can implement drag-and-drop effects, when the user presses the left mouse button on the eventtrapper_btn and drag, when the mouse released outside the EVENTTRAPPER_BTN, Releaseoutside event will occur, In this event we set the position of the EVENTTRAPPER_BTN (defined by _x and _y coordinates) to the position of the mouse when the current mouse is released, and the button moves.
The above is by dragging the mouse to move the object, the following try to use the keyboard to implement, select Evnettrapper_btn, for it to append such code.
On (keypress " ") {
eventtrapper_btn._x = eventtrapper_btn._x-6
}
On (keypress " ") {
eventtrapper_btn._x = eventtrapper_btn._x+6
}
On (keypress " ") {
Eventtrapper_btn._y = eventtrapper_btn._y-6
}
On (keypress " ") {
Eventtrapper_btn._y = eventtrapper_btn._y+6
}
The role of these four events is obvious when the user presses the left arrow (keypress " Event), the value of the eventtrapper_btn._x is reduced by 6 units, and so on, and so on, the other code implications are easy to understand.
2.2.3 Movie clips and buttons
As you can see from the previous example, an instance of a movie clip can also have its own event-handling code. However, you must be aware of the following issues when using movie clip instance events:
We can add events that were originally captured by buttons for the movie clip instance, such as rollover, rollout, and so on. However, in particular, the movie clip instance can catch such events, but in the processing of these events we cannot directly refer to other objects, generally we only modify the properties of the movie clip itself, such as the previous movie clip Instance DRAGTEST_MC processing event is an example. We can modify the transparency of the movie clip instance dragtest_mc in the handling of this event, but don't expect to simply add the message_txt.text= "" code to modify the content of the feedback area. In the process of execution, such code is not effective, and flash does not complain, which often makes beginners feel dizzy. There are two solutions, one is to use the button instead of film clips, followed by a small change to the code above, changed to: _root.message_txt.text= "", so that the program can be implemented. _root is a built-in object provided by Flash that allows you to accurately position elements on an interface.
When a movie clip instance is given a mouse event, the mouse pointer appears as a small hand above it, and in order to avoid this, you can let it catch the rollover event and join the code.
On (rollover) {
This.usehandcursor = false;
}
Usehandcursor This property is to set whether the hand pointer is displayed when the mouse hovers over the current object, and the default value is true, which is to display a hand pointer, which is set to false and is not displayed.
We can specify a name for the button instance (the suffix is generally _btn), but there is no illusion that there are many differences between the button instance and the movie instance. The most important point is that the button does not have its own timeline, and the movie clip has its own timeline. What do you mean by that? To put it simply, this difference is reflected in the use of this. For example, if we add such event-handling code for a movie clip instance:
On (Press) {
This._rotation = 30;
}
When you click the left mouse button on the movie clip instance, the movie clip instance will rotate. But if you give such code a button instance, when you click the button, it will not rotate the button itself but the button's parent object. In many cases, this strange phenomenon makes it confusing to confuse users with a slight difference between a button instance and a movie clip instance.
Let's take a look at one of the previous examples.
On (releaseoutside) {
eventtrapper_btn._x = _root._xmouse;
eventtrapper_btn._y = _root._ymouse;
}
In this line of code, you might think eventtrapper_btn._x such a statement too cumbersome, with this._x more convenient direct? If this time processing code is given to a movie clip instance, there is no problem with this, but in the previous example, this code is provided to the button instance, so it must be clear and clear to move the object, otherwise the effect of the operation will be contrary to our prior assumptions.
If you need to use a movie clip instead of a button, consider adding a special tag inside it (_UP, _over, _down), and then write the appropriate code. In addition, each button will have a "hot zone", that is, click a valid area, usually the button's graphics coverage, if you need to modify the range of this area can use the movie clip instance of the Hitarea properties, such as
Myclipbutton_mc.hitarea = _ROOT.MYHITCLIP_MC;
Overall, the button is enough to implement, the movie clip can be implemented, and the movie clip can implement the function button may not be able to achieve. What's the use of a button? This is mainly due to the fact that the buttons are the most frequently used elements of the program interface, and providing specialized button types can improve the efficiency of the design.

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.