我試圖在列表中存儲一系列GameObjects。由於某種原因,任何時候我把一個對象放在列表中,它就會失去它的精靈引用(變爲null)。所有其他對象數據(位置,顏色等)似乎都停留在對象中。這是我一直在嘗試:放置在列表中時玩家對象會丟失紋理
static class Global
{
public static List<GameObject> objects = new List<GameObject>();
}
這是我正在使用的列表。現在有問題的對象 - 玩家:
class Player : GameObject
{
public Vector2 position = Vector2.Zero;
public Texture2D sprite;
public Color image_blend = Color.White;
public Player() : base()
{
//nothing here, nothing in base class either
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(sprite, position, image_blend);
}
}
最後,在我的主要XNA類(重要片段):
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
sprPlayer = Content.Load<Texture2D>("player");
player = new Player();
player.sprite = sprPlayer;
Global.objects.Add(player);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
for (int i = 0; i < Global.objects.Count; i++)
{
Global.objects[i].Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
我有一種感覺,我可能要對這個錯誤的方式。任何幫助表示讚賞。
我想我發現了這個問題。我沒有在'GameObject'中聲明'Draw'和'Update'方法爲'virtual',所以它們沒有被Player類正確覆蓋。謝謝你的幫助! – 2012-02-09 05:39:56