在多维数组中查找对象索引



我正在制作库存并添加堆栈,但遇到问题

下面的

是我想要的与

相比

我只想找到经过

的对象的索引

myArray[0] = [item:object,StackAmmount:int]

        var myArray:Array = new Array();
        myArray[0] = ["name1",1];
        myArray[1] = ["name2",1];
        myArray[2] = ["name3",1];
        trace("Name" , myArray[0][0]);
        //traces "name1"
        trace("Stack" , myArray[0][1]);
        //traces "1"
        trace("Index of Object" , myArray.indexOf("name2"));
        //traces -1
        // Not Working (NOT FOUND)
        //How can I find the index of "item1" or "item2" in the above example


        var myOtherArray:Array = new Array();
        myOtherArray[0] = "name1";
        myOtherArray[1] = "name2";
        myOtherArray[2] = "name3";
        trace("Name" , myOtherArray[0]);
        //traces "name1"
        trace("Index of Object" , myOtherArray.indexOf("name2"));
        //traces 1
        //Working

也许有更好的方法处理堆栈?

粘贴Bin链接:http://pastebin.com/CQZWFmST

我将使用自定义的class,因此1D向量就足够了。class将包含项目的name和堆栈。您可以将此class子类化为override(项目的maxStack变量),然后搜索也会更容易,您可以迭代vector并检查name

public class InventoryItem
{
    protected var _name:String;
    protected var _stack:int;
    protected var _maxStack:int;
    public function InventoryItem():void {
    }
    public function get name():String {
        return _name;
    }
    public function get stack():int {
        return _stack;
    }
    public function set stack(value:int):void {
        _stack = value;
    }
    public function get maxStack():int {
        return _maxStack;
    }
}
...
public class InventoryWeapon extends InventoryItem
{
    public function InventoryWeapon(__name:String, startStack:int) {
        _maxStack = 64;
        _name = __name;
        _stack = startStack;
    }
}

相关内容

  • 没有找到相关文章

最新更新