Removing displayobject from an array

Hi

i’m experimenting a bit and noticed that when i remove an object from an array as followed:

private var list:Array<Actor>;
private var coin: Coin;
//game class
public function init() {
        var x = 0;
        var y = 0;
        for (i in 0 ... 5) {
            x += 30; 
            y += 30;
            coin = new Coin(x, y, list);
            list.push(coin);
        }
        for (actor in list) {
            addChild(actor);
        }
    }

//sprite class
private function kill(event: MouseEvent): Void {
     /*
         list is referenced to an array of sprites  -
         this is an instance of a sprite within the list - AKA the coin
    */ 
     list.remove(this); 
}

everything works as expected. The object is removed from the list as can be seen in the output from:

trace(list.length)

However, the object(sprite that is drawn) still can be seen(its been drawn on the screen) even when the instance to that object has been removed.

Have I missed something?

If I understand correctly, you might also need something like:

if (parent != null) parent.removeChild (this);
1 Like

Though usually, I do the removeChild and the list removal from the parent, not from the child object, so in your Game class you could do something like:

public function removeActor (actor:Actor):Void {
    
    removeChild (actor);
    list.remove (actor);
    
}

Just don’t call it twice

1 Like

Ah fair enough. That makes sense, I’m terribly sorry by my poor explanation.
I’ve had a long day with a lot of bug fixing.

Thank you.

No problem :success: