woochy
woochy

Reputation: 73

Haxe Typedef Array?

I'm very new to Haxe, and trying to make a simple tile-map creation program with OpenFL. However, I'm not sure how to make an array of classes (each individual tile types) I have made. It seems that a typedef is what I want, but I'm not sure how to incorporate this into an array so I can iterate through them. Thanks in advance, - RealFighter64

Upvotes: 1

Views: 562

Answers (1)

Andy Li
Andy Li

Reputation: 6034

I assume the tile classes are all subclasses of a base class. In this case, just put them into an Array<Class<Base>> as follows:

class Base {

}

class A extends Base {
    public function new():Void {}
}
class B extends Base {
    public function new():Void {}
}
class C extends Base {
    public function new():Void {}
}

class Test {
    static function main() {
        var classArray:Array<Class<Base>> = [A, B, C];
        for (cls in classArray) {
            var inst = Type.createInstance(cls, []);
        }
    }
}

If they do not have a common super class, use an Array<Dynamic> instead.

Upvotes: 3

Related Questions