2016-05-21 43 views
0

我仍然是編程方面的新手,我目前正在嘗試爲測試遊戲製作菜單。然而,我有一個問題,當我使用spriteBatch.DrawString從Main.cs中的類Language.cs中引用一個值時,它返回一個ArgumentNullException。任何幫助將不勝感激。XNA:引用公共類中的值時引發ArgumentNullException

調試器突出了我的Main.cs DrawMainMenu在以下段():

spriteBatch.DrawString(UIFont, Language.mainMenu[0], new Vector2(mainWidth/2, mainHeight/2 - 192), Color.White); 

我不知道這是否是我用什麼樣的順序我的方法的問題,但這裏是什麼Main.cs的整個頁面看起來像。請原諒一些亂七八糟的事情,因爲我一直在試驗這個程序中的各種東西。

public class Main : Game 
{ 
    GraphicsDeviceManager graphics; 
    public static SpriteBatch spriteBatch; 
    public static SpriteFont UIFont; 
    public static Texture2D logo; 
    public static Texture2D titleBack; 
    public static Texture2D titleSelect; 
    public static int menuType; 
    public static Texture2D cursor1; 
    public static MouseState mouseState; 
    public static MouseState mouseStatePrevious; 
    public static MouseState oldState; 
    public static MouseState newState; 
    public static int mouseX = mouseState.X; 
    public static int mouseY = mouseState.Y; 
    public static Vector2 cursorPos; 
    public static int strength = 0; 
    public static int mainWidth = 1920; 
    public static int mainHeight = 1080; 
    public static bool showSplash = true; 
    public int splashCounter; 
    public static int fadeCounter; 
    public static bool gameTimeActive = false; 

    private static int maxMenuItems = 12; 
    public static int selectedMenu = -1; 
    public static int selectedMenuType = -1; 
    public static bool mainMenu = false; 
    public static Vector2 screenPosition; 

    Player player = new Player(); 

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

     graphics.PreferredBackBufferWidth = mainWidth; // set this value to the desired width of your window 
     graphics.PreferredBackBufferHeight = mainHeight; // set this value to the desired height of your window 
     //graphics.PreferredBackBufferWidth = GraphicsDevice.DisplayMode.Width; 
     //graphics.PreferredBackBufferHeight = GraphicsDevice.DisplayMode.Height; 
     graphics.ApplyChanges(); 
    } 

    // Allows the game to perform any initialization it needs to before starting to run. 
    // This is where it can query for any required services and load any non-graphic 
    // related content. Calling base.Initialize will enumerate through any components 
    // and initialize them as well. 

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

     player.Initialize(); 
     base.Initialize(); 
    } 

    // LoadContent will be called once per game and is the place to load 
    // all of your content. 
    protected override void LoadContent() 
    { 
     // Create a new SpriteBatch, which can be used to draw textures. 
     // Load initial content... 

     spriteBatch = new SpriteBatch(GraphicsDevice); 
     logo = Content.Load<Texture2D>(@"Textures\StudioLogoW"); 

     UIFont = Content.Load<SpriteFont>(@"Textures\Fonts\Font_FrontEnd"); 
     cursor1 = Content.Load<Texture2D>(@"Textures\CursorWhite"); 
     titleBack = Content.Load<Texture2D>(@"Textures\UI\TitleBackground"); 
     titleSelect = Content.Load<Texture2D>(@"Textures\UI\TitleSelect"); 


     player.LoadContent(Content); 

     // TODO: use this.Content to load the rest of the game content... 


    } 


    // UnloadContent will be called once per game and is the place to unload 
    // all content. 
    protected override void UnloadContent() 
    { 
     // TODO: Unload any non ContentManager content here 
    } 

    // Allows the game to run logic such as updating the world, 
    // checking for collisions, gathering input, and playing audio. 
    // <param n 
    protected override void Update(GameTime gameTime) 
    { 
     // Enable mouse funtion in game. 
     //this.IsMouseVisible = true; 

     cursorPos = new Vector2(mouseState.X, mouseState.Y); 

     mouseState = Mouse.GetState(); 

     base.Update(gameTime); 

     // Get Mouse State, in this case, we're checking to see if a button was clicked, and which one. 
     // Depending on which button was pressed, it will either add or subract strength. 
     if (gameTimeActive) 
     { 
      player.Update(gameTime); 

      if (mouseState.LeftButton == ButtonState.Pressed && mouseStatePrevious.LeftButton == ButtonState.Released) 
       strength++; 
      if (mouseState.RightButton == ButtonState.Pressed && mouseStatePrevious.RightButton == ButtonState.Released) 
       strength--; 

      if (strength > 255) 
       strength = 255; 
      if (strength < 0) 
       strength = 0; 

      mouseStatePrevious = mouseState; 
     } 


     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     //Add your update logic here 
    } 

    protected void DrawSplash(GameTime gameTime) //Section for drawing our splash logo, and fading it in and out. 
    { 
     base.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black); 
     base.Draw(gameTime); 
     Main.spriteBatch.Begin(); 
     this.splashCounter++; 
     Microsoft.Xna.Framework.Color white = Microsoft.Xna.Framework.Color.White; 
     byte splashByte = 0; 
     if (this.splashCounter <= 75) 
     { 
      float splashNum = (float)this.splashCounter/75f * 255f; 
      splashByte = (byte)splashNum; 
     } 
     else if (this.splashCounter <= 225) 
     { 
      splashByte = 255; 
     } 
     else if (this.splashCounter <= 300) 
     { 
      int splashNum2 = 225 - this.splashCounter; 
      float splashNum3 = (float)splashNum2/75f * 255f; 
      splashByte = (byte)splashNum3; 
     } 
     else 
     { 
      Main.showSplash = false; 
      Main.mainMenu = true; 
      Main.selectedMenu = 0; 
      Main.fadeCounter = 75; 
     } 
     white = new Color((int)splashByte, (int)splashByte, (int)splashByte, (int)splashByte); 
     Main.spriteBatch.Draw(Main.logo, new Rectangle(0, 0, Main.mainWidth, Main.mainHeight), white); 
     Main.spriteBatch.End(); 
    } 
    protected void DrawMainMenu() //Section for drawing our Main Menu and fading it in after the splash logo. 
    { 
     Language.lang = 1; 
     graphics.GraphicsDevice.Clear(Color.Black); 

     // Display some stuff. In this case, we're displaying the logo and some text. 
     spriteBatch.Begin(); 

     splashCounter++; 
     Microsoft.Xna.Framework.Color white = Microsoft.Xna.Framework.Color.White; 

     spriteBatch.Draw(titleBack, new Rectangle(0, 0, mainWidth, mainHeight), Color.White); 
     //spriteBatch.DrawString(UIFont, "Strength: " + strength, new Vector2(mainWidth/2, 50), Color.White); 
     //player.Draw(spriteBatch); 

     if (selectedMenu == 0) 
     { 
      spriteBatch.DrawString(UIFont, Language.mainMenu[0], new Vector2(mainWidth/2, mainHeight/2 - 192), Color.White); 
     } 

     spriteBatch.Draw(cursor1, cursorPos, Color.White); 
     spriteBatch.End(); 

     //base.Draw(gameTime); 

    } 
    // This is called when the game should draw itself. 
    // <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Draw(GameTime gameTime) 
    { 
     if (Main.showSplash) 
     { 
      DrawSplash(gameTime); 
      return; 
     } 

     if (Main.mainMenu) 
     { 
      gameTimeActive = false; 
      DrawMainMenu(); 
      return; 
     } 
    } 
} 

這裏是我的Language.cs:

public class Language 
{ 
    public static int lang = 0; 
    public static string[] mainMenu = new string[99]; 
    public static string[] debugMenu = new string[99]; 

    public static void MenuStrings() 
    { 
     if (lang == 1) 
     { 
      //For unavailable functions 
      mainMenu[99] = "Currently unavailable"; 
      //Main Menu 
      mainMenu[0] = "Single Player"; 
      mainMenu[1] = "Multiplayer"; 
      mainMenu[2] = "Options"; 
      mainMenu[3] = "Credits"; 
      mainMenu[4] = "Exit"; 
      //Single Player - Character 
      mainMenu[5] = "New Character"; 
      mainMenu[6] = "Load Character"; 
      //Single Player - World 
      mainMenu[7] = "New World"; 
      mainMenu[8] = "Load World"; 

      //Multiplayer - Front 
      mainMenu[9] = "Host"; 
      mainMenu[10] = "Join"; 
      //Multiplayer - Host 
      mainMenu[11] = "Game Mode"; 
     } 
    } 
} 
+0

你有沒有試過調試器?你發佈的代碼越多,人們提供幫助的可能性就越小。嘗試將代碼縮小到發生錯誤的地方 – Flaugzig

+0

啊,我的歉意。我對編程社區也相當陌生,所以我在教育方面有點欠缺。希望我最近的修訂有所幫助。 – Svard

回答

0

你永遠不會調用你的Language.MenuStrings()方法,所以Language.mainMenu只包含99個空值。

+0

* Derp *我有點尷尬,忘記了最基本的編程基礎之一。謝謝一堆! – Svard

+0

好吧,RIP我。我什至不能找出一個合適的方式來稱呼它!或者我將不得不重建整個事情?我不能僅僅創建一個新的Language.MenuStrings(),並且只需鍵入Language.MenuStrings();在我的DrawMainMenu()下不起作用。 – Svard

+0

@Svard我不知道你打哪個電話,所以很難說。考慮使用目前爲止的內容創建一個新問題,或者考慮在codereview站點中發佈這個問題。 – Saeed

0

從代碼中你已經證明,你從來沒有實際調用Language.MenuStrings。這意味着Language.mainMenu[0]在您訪問它時仍爲空引用。

相關問題