Skip to main content
Mr. Baker the Shoe Maker
Inspiring
June 14, 2013
Question

Creating a Class that Removes a MovieClip

  • June 14, 2013
  • 1 reply
  • 2877 views

I would like to create a class that removes a movie clip as this is something that needs to be done frequently.

I have a button and in the library I have assigned it to the "Close" class (AS Linkage).

I created the following class:

package  {

    import flash.display.MovieClip;

    import flash.display.SimpleButton;

    import flash.events.MouseEvent;

    public class Close extends MovieClip

    {

        //ContructorCode

        public function Close()

        {

            closeButtonNow();

        }

        public function closeButtonNow():void

        {

            this.removeChild(this);

        }

    }

}

Depending on how I  play with the code I get the following errors:

1061: Call to a possibly undefined method removeChild through a reference with static type Close.

5000: The class"Close" must subclass 'flash.display.SimpleButton' since it is linked to a library symbol of that type.

What am I doing wrong? I know it's something simple but I don't fully  understand the use of "this".

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
June 14, 2013

1061: You probably need to import the class that defines the removeChild method.  I believe it will be the DisplayObjectContainer class, but you can look into it to see.

5000: The error appears to be telling you the problem fairly directly.  Your class is extending the MovieClip class, but you are applying it to a SimpleButton.  Either change it to extend the SimpleButton class or use a MovieClip instead of a button.

Mr. Baker the Shoe Maker
Inspiring
June 14, 2013

Ned,

I changed the code to:

package  {

    import flash.display.MovieClip;

    import flash.display.SimpleButton;

    import flash.events.MouseEvent;

    import flash.display.DisplayObjectContainer;

   

    public class Close extends SimpleButton

    {

        //ContructorCode

        public function Close()

        {

            closeButtonNow();

        }

       public function closeButtonNow():void

        {

            this.removeChild(this);

        }

    //end of class

    }

}

But I get an error that says:

1061: Call to a possibly undefined method removeChild through a reference with static type Close.

Ned Murphy
Legend
June 14, 2013

Rothrock,

I tried that but "parent.removeChild(this);" actuallly removes the button from the movieclip and the user has no way of exiting or removing the movieclip.

Thanks anyway.


The Close class needs some reference to the object you want to close.  When you target "this", you are targeting the class object... the button itself.  So what is the object that you want to close relative to the button?