2013-10-04 114 views
0

我试图在我正在制作的游戏中使用数组进行清点。将变量与数字结合起来

我需要的是一些具有可变结合的方式,这样的事情:

itemBoxNumber = "itemBox" + currentItemBox; 
//In this case itemBoxNumber would say itemBox1 

,我可以用它来代替itemBox1。

function itemsMenuUpdate():void 
    { 
    for (var a:int = 0; a<maxInventory; a++){ 
       var currentItemBox:Number = 1; 
       if(~inventory.indexOf("Potion")){ 
       mainMenu.itemBox1.gotoAndStop("Potion"); 
      } 
      if(~inventory.indexOf("Hi-Potion")){ 
       mainMenu.itemBox1.gotoAndStop("Hi-Potion"); 
      } 
     } 
    } 

我只能找到AS2工作方法。任何帮助与此赞赏。

+0

您可以使用向量(或阵列)来存储你需要访问的所有对象,而t母鸡使用currentItemBox作为访问Vector中特定对象的索引。 – jpop

回答

0

如果我没有记错,可以在AS3中调用String的方法。

举例来说,如果你要调用的方法itemBox1mainMenu你可以这样做:

mainMenu["itemBox1"] 

这是一样的:

mainMenu.itemBox1 

所以,在你的榜样,你可以这样做:

mainMenu[itemBoxNumber].gotoAndStop("Potion"); 
+0

感谢这为我工作。 – Nathalis

0

您可以通过名称(您需要先设置)检索子显示对象。所以:

var itemBoxNumber = "itemBox" + currentItemBox; 
mainMenu.getChildByName(itemBoxNumber).gotoAndStop("Potion"); //TODO ensure the child was given such a name 
0

制作一个LabelNumber类,它同时包含一个String标签和一个Number。然后,您可以添加一个方法,该方法返回标签的字符串和组合的数字,同时保持值彼此独立。

相关问题