2016-04-20 138 views
1

我其實是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); 
    } 
} 

回答

0

簡單真的
你一定要知道的Game1函數的調用順序。首先,調用Game1構造函數。這就是你放置mainMenu = new MainMenu();的地方。之後去InitializeLoad等。您必須將mainMenu = new MainMenu();從Game1構造函數移動到Load函數,在Resources.LoadContent(Content);之後,因此在使用MainMenu之前可以加載資源。你Game1.cs代碼應該是這樣的:

public class GameMain : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 

    MainMenu mainMenu; 

    public GameMain() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 

     this.IsMouseVisible = true; 
    } 

    protected override void Initialize() 
    { 
     // TODO: Add your initialization logic here 

     base.Initialize(); 
    } 

    protected override void LoadContent() 
    { 
     // Create a new SpriteBatch, which can be used to draw textures. 
     spriteBatch = new SpriteBatch(GraphicsDevice); 

     // TODO: use this.Content to load your game content here 
     Resources.LoadContent(Content); 
     mainMenu = new MainMenu(); 
    } 

    protected override void UnloadContent() 
    { 
     // TODO: Unload any non ContentManager content here 
    } 

    protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     // TODO: Add your update logic here 
     base.Update(gameTime); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     // TODO: Add your drawing code here 
     spriteBatch.Begin(); 
     mainMenu.Draw(spriteBatch); 
     spriteBatch.End(); 
     base.Draw(gameTime); 
    } 
} 
+0

謝謝你的工作! 我不知道我可以這樣做,我一直在尋找一個視頻,其中的人在遊戲構造函數中聲明瞭他的菜單,我發現它並不總是這樣,當我考慮它時是邏輯:D –

+0

@JeanMeier人可能沒有資源類:D順便說一句,這是一個很好的概念! (你可以使它靜態)資源類。保持! – Monset

0

雖然我沒有看到你的遊戲類我的第一個假設是,你永遠不會調用公共靜態無效LoadContent(ContentManager內容)裏面的方法資源類。由於此功能正在實例化您的紋理,因此可能不會被調用。

+0

我認爲這將這個遺憾,我已經添加了遊戲級以上:)之後添加了新的消息! –

0

謝謝您的回覆! 的確,我忘了包含Game類,這是調用資源的正確方式嗎?

public class GameMain : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 

    MainMenu mainMenu; 

    public GameMain() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 

     this.IsMouseVisible = true; 

     mainMenu = new MainMenu(); 
    } 

    protected override void Initialize() 
    { 
     // TODO: Add your initialization logic here 

     base.Initialize(); 
    } 

    protected override void LoadContent() 
    { 
     // Create a new SpriteBatch, which can be used to draw textures. 
     spriteBatch = new SpriteBatch(GraphicsDevice); 

     // TODO: use this.Content to load your game content here 
     Resources.LoadContent(Content); 
    } 

    protected override void UnloadContent() 
    { 
     // TODO: Unload any non ContentManager content here 
    } 

    protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     // TODO: Add your update logic here 
     base.Update(gameTime); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     // TODO: Add your drawing code here 
     spriteBatch.Begin(); 
     mainMenu.Draw(spriteBatch); 
     spriteBatch.End(); 
     base.Draw(gameTime); 
    } 
} 
相關問題