我仍然是编程的新手,我目前正在尝试为测试游戏制作菜单。但是,我遇到一个问题,当我使用spriteBatch.DrawString在我的Main.cs中引用类,Language.cs中的值时,它返回一个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;
}
}
}
这是我的语言:
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 :(得分:0)
根据您显示的代码,您实际上从未致电Language.MenuStrings
。这意味着Language.mainMenu[0]
在您访问它时仍然是一个空引用。
答案 1 :(得分:0)
您从未调用过您的Language.MenuStrings()方法,因此Language.mainMenu只包含99个空值。