[英]Referencing Numbered Variables and Arrays
I am pretty new to AS3 and am trying to get an advanced inventory system going. 我对AS3还是很陌生,并且正在尝试使高级库存系统运行起来。 I have 3 players and 3 classes: PlayerStats1
, PlayerStats2
, and PlayerStats3
. 我有3个玩家和3个类: PlayerStats1
, PlayerStats2
和PlayerStats3
。 Inside each of these classes I have an array. 在每个此类中,我都有一个数组。 This array contains the inventory for each player. 该数组包含每个玩家的清单。 The arrays are all named inventory
. 这些数组都命名为inventory
。 I also have a variable that determines whose inventory is opened. 我还有一个变量,用于确定打开谁的库存。 This variable is just called turn
. 此变量仅称为turn
。 Now what if I wanted to find out and display what was in Player2's inventory slot 5 (the fifth value in PlayerStats2.inventory
)? 现在,如果我想找到并显示什么是Player2的库存槽5(在第五值什么PlayerStats2.inventory
)? How would I go about doing that with a simple code? 我将如何用一个简单的代码来做到这一点? Something like gotoAndStop(PlayerStat + turn.inventory[4]);
像gotoAndStop(PlayerStat + turn.inventory[4]);
. 。 Of course the code probably looks a lot bigger than that. 当然,代码看起来可能比这大得多。
Here is an example 这是一个例子
class A {
private var playDict:Dictionary = new Dictionary();
public function addPlayer(playerId:int, player:PlayStats):void {
playDict[playerId] = player;
}
public function getInventory(playerId:int, invertoryIndex:int):Object {
var player:PlayStats = playDict[playerId] as PlayStats;
if (player) {
if (player.inventory) {
return player.inventory[invertoryIndex];
} else {
return null;
}
} else {
return null;
}
}
}
PlayStats is base class of PlayStats1, PlayStats2, PlayStats3. PlayStats是PlayStats1,PlayStats2,PlayStats3的基类。 PlayStats1, PlayStats2, PlayStats3 could add target objects to its own invertory PlayStats1,PlayStats2,PlayStats3可以将目标对象添加到其自己的反相中
public class PlayStats {
public var inventory:Array = [];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.