G'day全部,创建一个游戏对象数组
如果我在屏幕上有一些对象称为敌人,我怎么能把它们放入一个数组? 我最初的想法是在创建它时创建它。我把一个声明,我Game.cs文件的顶部:
public enemy[] enemiesArray = new enemy[5];
后来,当我创造我试图将它们添加到阵列的敌人:
for (int i = 0; i < 2; ++i) // This will create 2 sprites
{
Vector2 position = new Vector2((i+1) * 300, (i+1) * 200);
Vector2 direction = new Vector2(10, 10);
float velocity = 10;
int ID = i;
Components.Add(new skull(this, position, direction, velocity, ID));
skullsArray[i] = skull; // This line is wrong
}
我也试图给它在使用这样的代码的update()方法:
foreach (GameComponent component1 in Components)
{
int index = component1.ID;
if (component1.ToString() == "enemy")
{
enemiesArray[index] = component1
}
}
但是落下来,因为COMPONENT1没有一个ID。我不得不假设,当程序通过每个GameComponent枚举时,它只能访问该组件的有限范围的数据。
最后,我希望能够提及我的敌人为敌[1],敌人[2]等
感谢, 安德鲁。
谢谢你。这正是我想要的。助教。 – 2012-01-13 12:29:19