Copy link to clipboard
Copied
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".
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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).
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
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.
Copy link to clipboard
Copied
Reread my response/question.
Copy link to clipboard
Copied
The object that I want to close is a movieClip which contains the Close button.
Copy link to clipboard
Copied
If you want to keep the code within the class, try using....
MovieClip(parent.parent).removeChild(MovieClip(parent));
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
Yeah, what Ned said.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Okay, thanks. I need to absorb the conversation a bit.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now