Flash MX/ActionScript Graphics Tutorial (14)

Source: Internet
Author: User
Tags execution
Tutorial

2.6 Event Monitoring
Event has a habit, is "clique", under normal circumstances, some objects are not receive some events, such as a dynamic text can not accept mouse events. If we write code that specifies the event-handling code for a dynamic text:

Mytextfield_txt.onmousedown = function () {

}

When we click on it, the code does not execute because it does not receive the mouse event at all. In order for it to accept mouse events correctly, you must add such code:

Mouse.addlistener (MyTextField);

This statement is to let MyTextField to mouse object "Report", after the execution of this statement, MyTextField has a "listening" mouse event function. In particular, it is important to note that once an object is registered as a mouse listener, it will accept all events emitted by the mouse, whether or not the mouse is over the object at the time of the event, and based on this principle, we make a small mouse monitor.

First, add two text to the scene, a static text above, a dynamic text below, and name the dynamic text mouseposition_txt.



Click on the last frame of the movie, open the Script panel, and enter this code:


The code is simple. The movie is ready to receive the event, next registers the Mousepostion_txt as a receive mouse event, and then uses the event-handler method to catch the OnMouseMove event and display the mouse position in the Mousepostion_txt when the event occurs , the execution effect of the above small program is as shown below.


In addition to listening to mouse events, we can also monitor key events and scene events:

Key.addlistener (Historyarray);

Stage.addlistener (Historyarray);

You can use RemoveListener to remove a listener after it has been used, for example:

Key.removelistener (Historyarray);

Mouse.removelistener (Historyarray);

Stage.removelistener (Historyarray);

2.7 Summary
Event handling is the foundation of interactivity and Autorun, and the events that ActionScript can handle are so numerous that we don't have to learn it one by one because most of the names and uses of these events are fairly intuitive. For example, processing mouse movement is onmousemove, processing mouse button is onmousedown, and so on, need to use the time to check the manual can easily handle. Only onclipevent events should pay attention to grasp, it is more flexible use. The end of this chapter also describes the method of specifying event-handling code for an object using event-handling functions, which should be handled as much as possible in this way to make your program structure clearer and easier to read.

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.