views:

550

answers:

2

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).
A: 

Hey, I can't make head or tail from the code. Where does submit_btn come from? Is it a property of the class? What about answer_txt?
You don't need to access e.currentTarget... to remove "this" simply:

this.parent.removeChild(this);
curro
when I trace e.current.Target.parent.parent, I do get the output [object MainTimeline]. However, when I try and remove a child from that timeline, I get Type Coercion failed: cannot convert global@3c0f30b1 to flash.display.DisplayObject. at MethodInfo-217(). Is there a way to get/convert this to the stage of the MainTimeline? (I also tried e.currentTarget.parent.parent.stage.removeChild(this) and that didn't work either.
staypuffinpc
I think you have to explain better what's on the stage, what you click and what you want to remove from the stage. You may not need two parents (e.currentTarget.parent.parent) but only one (e.currentTarget.parent.remove....) But again it's difficult to see what you're trying to do by reading your description. If you could be more precise: how do you add the objects to the stage, are they nested? etc
curro
I wish this.parent.removeChild(this) works, but it doesn't (it's the first thing I tried). this.parent.removeChild(this) returns the error "A term is undefined and has no properties at MethodInfo-217()"
staypuffinpc
I've added a bit to the original post to make it more clear what I'm trying to do. You can also view a sample at: http://studio.byuipt.net/testing/answerBoxes.swf
staypuffinpc
+1  A: 

Hey staypuffinpc,
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.

curro
With the help of a friends' example, I figured it out. The problem was my use of an anonymous function on the addEventListener method. It's something I've done a lot (AS3 only; I didn't develop much in AS2). For some reason, it breaks on my own computer.
staypuffinpc