So this has to be a bug? …
When listening for events on instances added from within a SWF, the instance names are NULL.
Consider this code:
var mc = new MainContainer();
private function onAdded(e:Event)
Now this gives the following output…
Test1.hx:127: [object Sprite]
The target.name property is null, but in the SWF assets this is not true…
This is not correct, the instancename should be readable…
var sp = e.target;
var sp:Sprite = e.target;
So the last solution works… but a find this a bit odd… anyone want to share their thoughts on this?
name uses a getter and setter. Let me check to be sure… yep, it does.
In Haxe, getters are a compile-time feature. When the compiler sees
sp.name, it’ll replace it with
sp.get_name(). At runtime,
sp.name doesn’t exist, but that’s usually ok.
The problem is, the compiler can only do the replacement if it knows what
sp is. If it doesn’t know
sp is a sprite, it can’t look up the variable declaration, so it can’t tell if
name uses a getter, and it leaves it as-is.
You may be used to type inference handling this for you. For instance, if you write
var sp = new Sprite(), Haxe infers that
sp is a sprite, and all the getters and setters will just work. However,
Dynamic, so Haxe can’t infer anything when you write
var sp = e.target.