Specifying mouse events for dynamically creating MovieClip

Source: Internet
Author: User
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!



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.