Access main stage from class definition file (as3)

actionscript-3

I'd like to access the stage of the main timeline from w/i a class that extends a movieclip. Basically, I have a button in the main timeline that makes a HUD appear. The HUD is an extended MovieClip class. When people click on a button in the HUD, I'd like to remove the object from the stage of the main MovieClip.

@curro: I think your confusion may come from the fact that I am running this code from a class definition file. Clicking on a button w/i this object should remove it from the DisplayList of the MainTimeline. Here's the code from the class definition file:

package classes {
    import flash.display.Stage;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class Answers extends MovieClip {
        public function Answers(){
            listen();
        }//constructor

            //initiatlize variables
        public var answersArray:Array = new Array();

        private function listen():void {
            submit_btn.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){
                answersArray.push(answer_txt.text);
                e.currentTarget.parent.parent.stage.removeChild(this);
            });//listen 
        }//listen

    }//class Definition
}//package

trace(e.currentTarget.parent.parent) gets me the MainTimeline, and trace(e.currentTarget.parent.parent.stage) appears to return the main stage, but I cannot use removeChild w/o getting an error that I am trying to coerce the stage to be a DisplayObject (which it ought to be).

What's on the stage of the MainTimeline: A single button that, when clicked, adds an instance of the Answers class to the stage.

What's part of the Answers class that's not in the code?
I first created Answers as a MovieClip object in the main library. It has 3 parts:

  1. a TextField named "answer_txt"
  2. a "clear_btn" that clears the answer_txt
  3. a "submit_btn" that submits the text of answer_txt and then removes the entire Answers object from the MainTimeline (at least, that's what I want it to do).

Best Answer

your class definition is really weird. Looks like a mixture of as2 and as3.
Try with this:

package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.text.*;
import fl.controls.Button;
public class Answers extends MovieClip
{

    public var answersArray:Array = new Array();

    public function Answers()
    {
        submit_btn.addEventListener(MouseEvent.CLICK, remove);
    }

    private function remove(e:MouseEvent)
    {
        answersArray.push(answer_txt.text);
        this.parent.removeChild(this);
    }

}

}

This works on my computer. Your code doesn't. I think it has something to do with the listen method. The class isn't still instatiated and you are making it work.

Related Topic