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

时间:2014-03-29 01:10:13

标签: arrays actionscript-3 flash multidimensional-array inventory

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

以下是我想要的东西

我只是想找到我通过的对象的索引

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

1 个答案:

答案 0 :(得分:0)

我会使用自定义class,因此一维矢量就足够了。 class将包含项目的name和堆栈。您可以将此class子类化为override项的maxStack变量,然后搜索也会更容易,您可以遍历vector并检查{ {1}}。

name
相关问题