我其實是XNA的新生,發現這個庫非常有趣,但我仍然缺乏一些知識去進一步,因爲我覺得我無法自己解決一個問題:(C#ArgumentNullException未處理(SpriteBatch.Draw)
spriteBatch.Draw()方法說我的紋理爲空,但是我已經將它加載到Resources.cs類中,並在MainMenu.cs中傳遞了紋理,所以我不知道問題出在哪裏,如果有人可以幫助我,我會非常感激!
Resources.cs
class Resources
{
public static Texture2D pixel;
public static Texture2D startButton, loadButton, quitButton;
public static SpriteFont consoleFont;
public static void LoadContent(ContentManager Content)
{
pixel = Content.Load<Texture2D>("Pixel");
consoleFont = Content.Load<SpriteFont>("Console");
// UI Ressources :
startButton = Content.Load<Texture2D>("UI/StartButton");
loadButton = Content.Load<Texture2D>("UI/LoadButton");
quitButton = Content.Load<Texture2D>("UI/QuitButton");
}
}
個
MainMenu.cs
class MainMenu
{
// Fields
List<Button> buttons = new List<Button>();
// Constructors
public MainMenu()
{
this.buttons.Add(new Button(new Vector2(480, 132), 256, 48, Resources.startButton));
this.buttons.Add(new Button(new Vector2(480, 212), 256, 48, Resources.loadButton));
this.buttons.Add(new Button(new Vector2(480, 292), 256, 48, Resources.quitButton));
}
// Methods
// Update
public void Update()
{
}
// Draw
public void Draw(SpriteBatch spriteBatch)
{
foreach (Button button in buttons)
{
button.Draw(spriteBatch);
}
}
}
Button.cs
class Button : UIElement
{
int width, height;
Texture2D texture;
public Button()
{
}
public Button(Vector2 b_position, int b_width, int b_height, Texture2D b_texture)
{
this.position = b_position;
this.width = b_width;
this.height = b_height;
this.texture = b_texture;
}
public void Update()
{
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, Color.White);
}
}
謝謝你的工作! 我不知道我可以這樣做,我一直在尋找一個視頻,其中的人在遊戲構造函數中聲明瞭他的菜單,我發現它並不總是這樣,當我考慮它時是邏輯:D –
@JeanMeier人可能沒有資源類:D順便說一句,這是一個很好的概念! (你可以使它靜態)資源類。保持! – Monset