Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Creating a Class that Removes a MovieClip

Engaged ,
Jun 14, 2013 Jun 14, 2013

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".

TOPICS
ActionScript
2.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 14, 2013 Jun 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 14, 2013 Jun 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 14, 2013 Jun 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).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 14, 2013 Jun 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 14, 2013 Jun 14, 2013

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 14, 2013 Jun 14, 2013

I know that I could create an instance of the button and just target that instance name. However, in my project I will use the "Close" button over 10 times. I don't want to have to create 10 instances of the "Close" button.

Capture3.PNG

I am attempting to create a class that will close (or remove a movieClip). In essence, it's a library button and in the "Symbol Properties" panel I have assigned the classs to "Close", with no Base Class. So the object that I want to close is a movieClip. These movieClips could be different everytime.

Capture.PNGCapture2.PNG

2.PNG

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 14, 2013 Jun 14, 2013

Reread my response/question.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 14, 2013 Jun 14, 2013

The object that I want to close is a movieClip which contains the Close button.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 14, 2013 Jun 14, 2013

If you want to keep the code within the class, try using....

MovieClip(parent.parent).removeChild(MovieClip(parent));

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 14, 2013 Jun 14, 2013

I think what Ned suggests should work. But it isn't good design to have a child close the parent — especially if you are going to use it in multiple locations.

In general I think it would be better to have the close button dispatch an event when it is clicked and then have the parent listen for that event and do what it needs to do on close. That way each different parent can do what it needs.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 14, 2013 Jun 14, 2013

That is why I premised my reply with "If you want to keep the code within the class"...

The more oop proper way splits the code up where some parent object somewhere (not necessarily the main timeline) has to carry the listener(s  - more than one button/mc is involved) and the function to perform the closure.

But if you have to take the time to add the listener code to the parent.parent for each movieclip that has one of these buttons to catch the dispatched event, then you might just as well skip that and the Close class and just add the listeners for the button clicks there instead.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 15, 2013 Jun 15, 2013

I would like to adhere to proper opp techniques. This is my first project where I'm using separate class files so I'm still learning. So how would I "keep the code within the class?" I don't quite understand; more specifically, the use of the dispatch event. The times that I have used this event it has been within a movieclip on the timeline. How would you propose I use this event when working with class files?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 15, 2013 Jun 15, 2013

You don't keep the code within the class if you go the more proper OOP route.  But the more proper OOP approach is more proper when you are dynamically adding content, not so much when you are manually planting it. 

The premise is that objects should not rely on other objects being present to function properly.  In this case, if you were to target a parent.parent object, that requires that parent.parent object to exist.  If you were to try to create a separate swf file that you intend to load into another, you would have a hard time compiling it due to the lack of a parent.parent object for the compiler to identify.

The more proper approach is to avoid that reliance on another object by simply treating the child as pretty much exactly that.  When a child wants something it cries, and the parent hears (listens) and takes an action as a result.

So in this case, what you would have is the movieclip button dispatch an event when it gets clicked.  That's all it does, it announces it wants something...  it cries...

       dispatchEvent(new Event("eventTriggered"));

The parent.parent object will have an event listener assigned (and its handler) for that event so that it can take the necessary action in response to that event, which in your case is to remove the movieclip.

       mcWithButton.addEventListener("eventTriggered", eventHandler);

       function eventHandler(event:Event):void {

             removeChild(event.currentTarget);

             //  use event.currentTarget to keep it generic and share

       }

If you happen to be adding these movieclips dynamically, then you can have the listeners assigned dynamically upon the mc's being loaded, and they can share the same removal function.

But, as you can see, you are back to coding outside of the class and in the main document somewhere.  So if you are manually placing these mc's on the stage, you could have skipped all of this and just added the event listener for the button in the main document instead.... basically back to where you were trying to get away from...

      mc.button.addEventListener(MouseEvent.CLICK, eventHandler);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 15, 2013 Jun 15, 2013

Yeah, what Ned said.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 15, 2013 Jun 15, 2013

If I hear you correctly, in my case, since the button resides within the movieclip (the movieclip is added dynamically), it (the button) is a child of the movie clip. You are saying that I should take the button out of the movie clip and add it to the stage dynamically, separate from the movieclip. This would be a more proper opp approach and allow more flexibility.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 15, 2013 Jun 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 15, 2013 Jun 15, 2013
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines