0

I have a main class in which I create instances of a class and add them with addChild(). I also have buttons and their respective classes. I want to remove the instance with removeChild() from my button class and not from the main class. Is that even possible?

One of the objects in mainClass:

public static var start_icon_:start_icon = new start_icon();
addChild(start_icon_);

I want to be able to:

removeChild(start_icon_); in a class that isnt mainClass.

  • Some code or visualization would be nice – Matthijs van Hest Sep 14 '15 at 13:42
  • Why are you using static? –  Sep 14 '15 at 13:47
  • I wanted to try removing it with removeChild(mainClass.start_icon_); – Dimitar Velev Sep 14 '15 at 13:48
  • 1
    You don't need to use static. The easiest solution would be to dispatch an Event when you press the button. The `mainClass` which created the instance of the class listens to this event. When the Event triggers, remove the instance. –  Sep 14 '15 at 13:51
  • @DodgerThud I thought about that but im not too sure how to add a listener to my button. I gave an instance name to the button called first_network_instance but first_network_instance.addEventListener(and so on) does not work. This button is inside a movieclip which I also add to the stage with addChild. Thats why I have a listener inside the class of my button. – Dimitar Velev Sep 14 '15 at 13:55
  • if you instantiate your `mainClass`, `mainClassInst.removeChild(start_icon_)` should do the trick. Another option is to add `start_icon_` to `stage` and then call `stage.removeChild(start_icon_)` – www0z0k Sep 14 '15 at 14:17
  • @www0z0k I get 1120: Access of undefined property start_icon_. when I do your first solution. I instantiate my mainclass by writing var main_object:mainClass = new mainClass(); and main_object.removeChild(start_icon_) does not work – Dimitar Velev Sep 14 '15 at 14:24
  • @DimitarVelev Why not just : `obj.parent.removeChild(obj);` ? – akmozo Sep 14 '15 at 14:50
  • @akmozo you mean main_object.parent.removeChild(start_icon_)? If so I get the same error – Dimitar Velev Sep 14 '15 at 14:53
  • @DimitarVelev I mean the `parent` of your inserted `child` ( the child here is `start_icon_` ). Try also to put an example to simplify things to others to help you ... And it's always more easy to remove an object from the class which has created it ... – akmozo Sep 14 '15 at 15:02
  • @DimitarVelev - akmozo is correct, you can simply remove the object by going through its own parent. Anywhere you have a reference to `start_icon_`, you can remove it through, `start_icon_.parent.removeChild(start_icon_);` Given your structure, you would have to `mainClass.start_icon_.parent.removeChild(mainClass.start_icon_);` – Joseph Sep 14 '15 at 15:42

1 Answers1

0

First, a class does not have a parent or child; an instance has a parent or child. It's a good idea to try to get used to thinking that way. When you make something static you are making the code associated with the class, which is not specific to any particular instance. Meanwhile, methods like addChild and removeChild are instance functions, because they necessarily relate to specific instances of the class. So when you try to do things like expose a function globally by making it static, but want to do instance specific things like removeChild, you can get rather tangled.

To solve your problem, there are a few solutions.

You can simply "reach upward" to remove the child. For example, parent.removeChild(this) will remove a child from its parent. If you know the parent is a MainClass instance, you can cast and reference a property on it: parent.removeChild(MainClass(parent).start_icon). (start_icon should not be static.) You can even reach parent.parent. and so on.

That's not a great solution, though, because you are making the code assume a certain parent/child hierarchy. If you move things around it will break at runtime. A better solution (already mentioned in the comments) is to use events. For example:

class MainClass extends Sprite {
    public var startIcon:StartIcon = new StartIcon();

    public function MainClass() {
        addChild(startIcon);
        addEventListener("removeStartIcon", removeStartIcon);
    }

    private function removeStartIcon(e:Event):void {
        removeChild(startIcon);
    }
}

// From start icon, or any child (or children's child, etc) of main class instance:
dispatchEvent(new Event("removeStartIcon", true)); // bubbles=true, to trickle up the display hierarchy

Lastly, you can use the singleton pattern. Like others, I don't recommend this, but it is a quick and easy way to make a single instance of a class behave globally. The gist is that you expose a static reference to a single instance of your class, with the assumption you only ever need a single instance of that class. Then you can reference instance functions and properties from anywhere through the static reference of the class to its single instance. For example:

class MainClass extends Sprite {
    public static var main:MainClass;
    public var startIcon:StartIcon = new StartIcon();
    public function MainClass() {
        main = this;
        addChild(startIcon);
    }
    public function removeStartIcon():void {
        removeChild(startIcon);
    }
}

Now from anywhere you can do this: MainClass.main.removeStartIcon() or even MainClass.main.removeChild(MainClass.main.startIcon).

Community
  • 1
  • 1
Aaron Beall
  • 49,769
  • 26
  • 85
  • 103