Event Listener Sample Rollup

Source: Internet
Author: User
Tags event listener
Example

The Listener-radio model for an event differs from the event-handling function method by allowing multiple snippets of code to listen to the same event in a conflicting manner.

Let's just say, it's like ordering a newspaper, we can go to the newspaper every 5 minutes, if you have plenty of time, or you can say hello to the owner of the newspaper store and tell him the newspaper is here. Obviously, the latter is far more efficient than the former, and in Flash the former is like this

This.onenterframe=function () {
if (paperarrived) {
SendMe ();
}
}

The result is that each frame has to verify that the paperarrived is true and that the efficiency is too low, but it can also solve the problem.

Use the event listener-radio model, just like this

var myobj=new Object ();
Myobj.onpaperarrived=function () {
SendMe ();
}
Me.addlisterner (myobj);

As soon as the newspaper arrived, execute SendMe (); This program, efficient AH!

Here is a look at demo, here I did not put the stage, because the comparison, usually use not much.


Demo

Main code (take mouse here for example):

This.createemptymovieclip ("Tmp_mc", this.getnexthighestdepth ());
var mymouseobj = new Object ();
Mymouseobj.onmousedown = function () {
if (_xmouse>400 && _xmouse<550 && _ymouse>0 && _ymouse<300) {
THIS.TARGET_MC = Tmp_mc.createemptymovieclip ("", Tmp_mc.getnexthighestdepth ()-10000);//NOTE: Here I've lost 10000, What's the difference, everyone can go back and try.
origin_x = _xmouse;
origin_y = _ymouse;
This. Drawing = true;
}
};
Mymouseobj.onmousemove = function () {
if (_xmouse>400 && _xmouse<550 && _ymouse>0 && _ymouse<300) {
if (this. Drawing) {
This.target_mc.clear ();
This.target_mc.lineStyle (1, 0xff0000, 100);
This.target_mc.moveTo (origin_x, origin_y);
This.target_mc.lineTo (_xmouse, _ymouse);
}
Updateafterevent ();
}
};
Msleep_btn._visible = false;
Mymouseobj.onmouseup = function () {
This. Drawing = false;
};
Mactive_btn.clickhandler = function () {
Mouse.addlistener (Mymouseobj);
Msleep_btn._visible = true;
Mactive_btn._visible = false;
};
Msleep_btn.clickhandler = function () {
Mouse.removelistener (Mymouseobj);
Msleep_btn._visible = false;
Mactive_btn._visible = true;
};

source file Download :addlistener.zip



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.