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 );