Create | dynamic | mouse
How to specify mouse events for dynamically created MovieClip
1. Under normal circumstances, you can specify an event directly by setting the event handler, and here is an example
IMAGEMC = Maincontainer.createemptymovieclip ("Imagemc", 1);
With (IMAGEMC)
{
MoveTo (0,0);
LineStyle (1,0XFFFF00);
Beginfill (0x00ff00,60);
LineTo (400,0);
LineTo (400,100);
LineTo (0,100);
}
Imagemc.onrelease = function () {
Trace ("AAA");
}
2. If the MovieClip is created, the Loadmovie operation is performed , for example, if a picture is loaded, the above method is invalid.
( Note : The use of other common event handler binding method, also will be ineffective, after N times after the failure and after a survey, finally in the flash help found a message)
"Use the event handler function with the Loadmovie (Movieclip.loadmovie method), and the result is unpredictable." If you use on () to attach an event handler to a button, or to create a dynamic handler function using an event-handling function method such as onpress (Movieclip.onpress), and then call Loadmovie (), after the new content is loaded, The event handler function will no longer be available. However, if you attach an event-handler function to a movie clip by using the onclipevent or on processing function, and then call Loadmovie () on the movie clip, the event-handler function is still available after the new content is loaded. ”
It's so silent. Flash officials encourage developers not to write code to objects, should be written to an external ActionScript file. However, Onclipevent () and on () are typically written to the object, and in the help of another page of Flash, it is clearly resisted. So if, in the pure. As file, there are ways to add events to the MovieClip after Loadmovie.
After a survey, finally found that can be solved with the following methods, can solve this problem, the solution is also more elegant, is the aid of Moviecliploader.
IMAGEMC = Maincontainer.createemptymovieclip ("Imagemc", 1);
Imagemc.loadmovie ("e:\\1.jpg"); Not in this way.
var loader:moviecliploader = new Moviecliploader ();
Loader.loadclip ("E:\\1.jpg", IMAGEMC);
var mcllistener:object = new Object ();
Mcllistener.onloadinit = function (evt:movieclip) {
evt.onpress = function () {
Trace ("BBB");
}
}
Loader.addlistener (Mcllistener);
In the Mcllistener evt object, set the Onpress event. Problem solved!