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.

Mr. Baker the Shoe Maker
Inspiring
June 15, 2013

Nope.  If you are adding the movieclip dynamically, and you want to follow the oop quidelines, then you leave the button inside the movieclip and you have it dispatch a custom event when it gets clicked.

When the mc gets added, you assign the event listener for the custom event.

You basically want to review/implement what I described in response 13, ignoring the last paragraph and line of code at the end.

But you may lose achieving your goal of not having to deal specifically with all of the mc's that have these buttons.  It all depends how you go about adding them dynamically.  If you can do that generically using the same code, then you avoid having to code for each one specifically.


Okay, thanks. I need to absorb the conversation a bit.