Flash/flex Study Notes (35): How to correctly listen to events of stage objects

Source: Internet
Author: User

If you want to register a listening event for the stage object in a custom class, and then use the instance of this class in another document class (or use the instance of this class on the FLA timeline ), you will be very depressed to find that this is never referenced in the constructor. stage (using trace (this. stge) always returns NULL). Since no reference is available, the event cannot be registered. The correct method is as follows:

Package {import flash. display. sprite; import flash. events. event; import flash. events. mouseevent; public class myclass extends sprite {public function myclass () {trace ("myclass constructor called... "); trace (this. stage); // output nulladdeventlistener (event. added_to_stage, addedtostagehandler);} function addedtostagehandler (E: Event): void {trace (this. stage); // you can obtain the stage reference this. stage. addeventlistener (mouseevent. mouse_down, mousedownhandler)} function mousedownhandler (E: Event): void {trace ("You clicked the stage ");}}}

That is, the stage object can be referenced only after the added_to_stage event. Of course, an instance of this type must be addchild in advance. For example, the following figure shows the frame in the FLA timeline.CodeIn the following example:

VaR mycls: myclass = new myclass (); addchild (mycls );

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.