-2

I'm having an issue with C# and XNA for Windows phone. I've created a class called Item which is inside of the Game1 class in XNA. The problem I am experiencing is that after I create all of the items I cannot make changes to them, it does not throw an error but also the change does not take effect. So the code goes something like this:

public class Game1 : Microsoft.Xna.Framework.Game {
  Item Loading;
  Item[] Stuff = new Item[2];

  public class Item {
    public bool visible = true;
  }

  protected override void LoadContent() {
    Loading = new Item();
    Stuff[1] = new Item();
  }

  protected override void Update(GameTime gameTime) {
    Loading.visible = false;
    System.Diagnostics.Debug.WriteLine(Loading.visible); //prints true
    Stuff[1].visible = false;
    System.Diagnostics.Debug.WriteLine(Stuff[1].visible); //prints false
  }
}

I don't understand why it is unable to change the variable, I have also tried doing this through an accessor method inside the class. I have basically duplicated code working for an array of Items, so that leaves me totally stumped.

4

1 に答える 1

0

このコードは機能します。継承の制約を削除しましたが、それらでも機能するはずです。私が見た主な問題は、あなたが「スタッフ」を宣言する方法でした。使用しているc#のバージョンはわかりませんが、私が知る限り(VS2010)、(C / C ++のように)自分が行った方法で配列を宣言することはできません。newキーワードを使用して配列を初期化する必要があります。

public class Game1
{
    Item Loading;
    Item[] Stuff;

    public class Item
    {
        public bool visible = true;
    }

    public void LoadContent()
    {
        Loading = new Item();
        Stuff = new Item[2];
        Stuff[1] = new Item();
    }

    public void Update(DateTime gameTime)
    {
        Loading.visible = false;
        System.Diagnostics.Debug.WriteLine(Loading.visible); //prints false

        Stuff[1].visible = false;
        System.Diagnostics.Debug.WriteLine(Stuff[1].visible); //prints false
    }
}
于 2012-10-15T13:43:56.300 に答える