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

Source: Internet
Author: User
Tags object implement interface modify
Programming | Tutorial 2.2 instance


briefly describes several common mouse events, let's 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 that displays feedback, EVENTTRAPPER_BTN is a button instance, used to capture 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, in this case we'll replace the mouse pointer with a custom shape. Enter 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 = "开始鼠标事件试验 ";

The first code is stop (), that is, let the movie play here to stop while waiting for the user's action, mouse.hide () Hide the mouse pointer, Immediately after StartDrag is the drag that starts the hand_mc of the movie clip instance, and because the mouse pointer is already hidden, the command now acts as if you were replacing 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 handle 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 = "鼠标浮动事件";
  }
  on (rollOut) {
   message_txt.text = "鼠标移出事件";
  }
  on (press) {
   message_txt.text = "鼠标单击事件";
  }
  on (dragOut) {
   message_txt.text = "鼠标在当前对象上按下左键后拖出";
  }
  on (release) {
   message_txt.text = "鼠标释放事件";
  }



these are relatively simple events, and then we'll deal with a slightly more complex event.


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.


above is the way to move the object by dragging the mouse, the following try to use the keyboard to implement, select Evnettrapper_btn, append such code for it.


  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 not difficult to understand.
2.2.3 Movie clips and buttons
From the previous example, you can see that an instance of a movie clip can 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, such as rollover, rollout, and so on, for the movie clip instance. 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 a 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 this 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.
to 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 is too cumbersome to use 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 the area can use the movie clip instance of the Hitarea property, such as
Myclipbutton_ Mc.hitarea = _ROOT.MYHITCLIP_MC;
In general, the ability to implement a button is sufficient, the movie clip can be implemented, and the feature buttons that the movie clip can implement may not be implemented. 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.

Cond......







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.