我正在製作農業/塔防遊戲,我在編程方面很新穎。我似乎在使用列表<>或XNA中的數組時遇到了一個主要問題。我無法讓它從列表中返回我想要的索引。C#列表和ArgumentOutOfRangeException的問題
主要問題是在我的種植引擎。我已經成功實施了一個種植系統,可以生成具有不同屬性的植物列表(spriteobjects),並將它們放置在地圖上。現在,我需要一種基於鼠標點擊該工廠的方式訪問工廠列表中的特定工廠。我覺得我非常接近,但是我最終得到了一個我無法解決的ArgumentOutOfRangeException。下面是代碼的演練:
初始化
public void Addplants()
{
switch (Mode)
{
case "Wotalemon":
NewPlant = new Plant(Texture, msRect);
NewPlant.AddAnimation("seed", 0, 16, 64, 64, 1, 0.1f);
NewPlant.AddAnimation("sprout", 64, 16, 64, 64, 1, 0.1f);
NewPlant.AddAnimation("wota", 128, 16, 64, 64, 1, 1.0f);
NewPlant.CurrentAnimation = "seed";
NewPlant.DrawOffset = new Vector2(32, 48);
NewPlant.Position = Position;
NewPlant.Type = "wotalemon";
NewPlant.Birthday = Days;
NewPlant.IsSelected = false;
plants.Add(NewPlant);
thisPlant = NewPlant;
//various plants after this
更新/繪製
我使用一些簡單的foreach循環在這裏更新並繪製植物,沒有任何問題。
的GetInfo(此方法使用spriteobject的擊中格屬性和mouseRectangle)
public void GetInfo(Rectangle ms)
{
msRect = ms;
for (int i = 0; i < plants.Count; i++)
{
foreach (Plant NewPlant in plants)
{
if (NewPlant.BoundingBox.Intersects(msRect))
{
SelectedIndex = i;
NewPlant.Tint = Color.Black;
}
else
NewPlant.Tint = Color.White;
}
}
}
最後,現在的問題是:
public void SelectPlant()
{
//if (SelectedIndex != null)
if (SelectedIndex > plants.Count | SelectedIndex < 0)
SelectedIndex = plants.Count;
SelectedPlant = plants[SelectedIndex];
}
唯一的例外是在這條線拋出:
調試器顯示值爲0.我嘗試過各種方法來嘗試預先發布索引從null。我覺得Getinfo()方法中的某些東西是關鍵。我相信我非常接近成功,因爲我在那裏插入的顏色測試非常完美。當我將鼠標懸停在植物上時,它會變黑,當我移除鼠標時,它會恢復正常。
這完全是我想要的行爲類型,除非我想將selectedIndex設置爲我在上進行移植的工廠的索引。任何建議將不勝感激。
if語句,'的SelectedIndex = plants.Count;'應該是'的SelectedIndex = plants.Count-1;' – asawyer 2012-04-14 03:10:31