0

林使庫存和進出口加堆棧,但香港專業教育學院擊中一個問題查找對象指數多維數組

下面

是我想對比一下什麼工作

我只是想找到對象的索引我穿過

myarray的[0] = [項目:對象,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 

也許有處理堆棧的更好的辦法?

粘貼斌鏈接:http://pastebin.com/CQZWFmST

回答

0

我會使用自定義class,因此一維向量就足夠了。 class將包含項目的name和堆棧。您可以將此class的子類別overridemaxStack變量的項目,然後搜索會更容易,您可以遍歷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; 
    } 
}