ADDED_TO_STAGE doesn't seem to fire on all children

I have an Entity class that is extending Sprite, and has a static manager. Here are the important bits of code:

class Entity extends Sprite {
	private var _registered:Bool;

	public function new() {
	    super();

	    _registered = false;

	    this.addEventListener(Event.ADDED_TO_STAGE, function(e) { _manager.register(this); _registered = true; });
	    this.addEventListener(Event.REMOVED_FROM_STAGE, function(e) { _manager.unregister(this); _registered = false; });
	}

	private static var _manager:EntityManager = new EntityManager();

	public static function ofType(type:String):Array<Entity> {
	    return _manager.ofType(type);
	}
}

My issue is, the REMOVED_FROM_STAGE events don’t fire for every entity when I remove their parent. So I might have a structure like this:

Scene
|-Entity
|-Entity
|-Entity

And if I remove the scene from the stage, not all of the Entities will have their REMOVED_FROM_STAGE event fired. I’ve checked the source and it looks like __dispatchChildren should hit all of them.

So I guess my question is, am I understanding correctly that all of the entities should have the REMOVED_FROM_STAGE event fired when their parent is removed from the stage? I’m having a hard time determining if the error is in my code or my understanding…

Ahh, I figured out what was going on.

When I registered ADDED_TO_STAGE and REMOVED_FROM_STAGE, there was not a one to one relationship on when they were called. Sometimes, ADDED_TO_STAGE can be called multiple times, where REMOVED_FROM_STAGE seems to always be called just once.

Odd behavior but I guess this is expected?

Hmm, that still sounds like a little bug. If there’s a simple way to reproduce, it would be nice to look into

OK. I’ll create a project to repro later today. Should I post here or github issues?

Either is fine, thank you! :grinning: