将对象存储在数组中(Haxe)

时间:2016-10-14 03:30:03

标签: arrays haxe haxeflixel haxelib

我如何在Haxe中将对象的新实例排序到数组中?

例如我有一个名为武器的类,在播放器类中我给出了一个数组库存。 那么我该怎么存储呢?

private void gun:Weapon

gun = new Weapon; //into the array

2 个答案:

答案 0 :(得分:3)

我认为你正在寻找这个:

private var inventory:Array<Weapon>;

这是Weapon类型的数组。 要添加内容,请使用push(),如下例所示:

class Test {
    static function main() new Test();

    // create new array
    private var inventory:Array<Weapon> = [];

    public function new() {
        var weapon1 = new Weapon("minigun");
        inventory.push(weapon1);

        var weapon2 = new Weapon("rocket");
        inventory.push(weapon2);

        trace('inventory has ${inventory.length} weapons!');
        trace('inventory:', inventory);
    }
}

class Weapon {
    public var name:String;
    public function new(name:String) {
        this.name = name;
    }
}

演示:https://github.com/justintv/Twitch-API/blob/master/authentication.md

答案 1 :(得分:1)

发现答案必须像这样写

private var inventory:Array;

武器是班级名称。