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