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
  • 2898 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.

Inspiring
June 14, 2013

I think you need to do something like:

parent.removeChild(this);

Of course you should probably also do something that checks to see if it is on the display list because tring to remove it if it isn't would throw and error (it wouldn't have a parent).