Haxe equivalent of getdefinitionbyname

I’m trying to do create random enemies, so I’ve got classes, Enemy1, Enemy2, Enemy3, …

In Flash I would have generated the number part randomly and then create the new class by using getDefinitionByName(“Enemy”+(rnd)).

I thought you could do this in Haxe by using Type, but that doesn’t appear to be the case.

Is there a way I can do this?
This is the apparent equivalent, (but doesn’t work.)

		var rnd:Int = Std.int(1+Math.random()*3);

		var eType:String = "dropSeven.game.Enemy"+rnd;

		var EnemyClass = Type.resolveClass(eType);
		var p:Enemy = Type.createInstance(EnemyClass, null);

You shouldn’t pass null to createInstance but [].

createInstance<T> (cl:Class<T>, args:Array<Dynamic>):T
If cl or args are null (…) the result is unspecified.


Thanks - I guess I was close (but no cigar!)

Could it be that your classes are being DCE’d? Add a @:keep to the top of the class, eg:

class something {

Or import it somewhere else in the app… Or turn off DCE altogether.