我找不到为什么它不起作用。先谢谢了。抱歉,代码量很大,我不确定错误在哪里。问题是它只显示黑屏和加载鼠标。它不显示加载屏幕或菜单。
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteBatch mBatch;
Texture2D mTheQuantumBros2;
MenuComponent menuComponent;
public static Rectangle screen;
public static string GameState = "Menu";
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferMultiSampling = false;
graphics.IsFullScreen = true;
graphics.PreferredBackBufferWidth = 1366;
graphics.PreferredBackBufferHeight = 768;
graphics.ApplyChanges();
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
mBatch = new SpriteBatch(this.graphics.GraphicsDevice);
ContentManager aLoader = new ContentManager(this.Services);
aLoader.RootDirectory = "Content";
mTheQuantumBros2 = aLoader.Load<Texture2D>("TheQuantumBros2") as Texture2D;
menuComponent.LoadContent(Content);
}
protected override void UnloadContent()
{
}
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
switch (GameState)
{
case "Menu":
menuComponent.Update(gameTime);
break;
}
base.Update(gameTime);
}
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
switch (GameState)
{
case "Menu":
menuComponent.Draw(spriteBatch);
break;
}
base.Draw(gameTime);
}
}
}
其他类:
class MenuComponent
{
KeyboardState keyboard;
KeyboardState prevKeyboard;
SpriteBatch mBatch;
Texture2D mTheQuantumBros2;
GameTime gameTime;
MouseState mouse;
MouseState prevMouse;
SpriteFont spriteFont;
List<string> buttonList = new List<string>();
int selected = 0;
public MenuComponent()
{
buttonList.Add("Campaign");
buttonList.Add("Multiplayer");
buttonList.Add("Zombies");
buttonList.Add("Quit Game");
}
public void LoadContent(ContentManager Content)
{
spriteFont = Content.Load<SpriteFont>("Font");
}
public void Update(GameTime gameTime)
{
keyboard = Keyboard.GetState();
mouse = Mouse.GetState();
if (CheckKeyboard(Keys.Up))
{
if (selected > 0) selected--;
}
if (CheckKeyboard(Keys.Down))
{
if (selected < buttonList.Count - 1) selected++;
}
prevMouse = mouse;
prevKeyboard = keyboard;
}
public bool CheckMouse()
{
return (mouse.LeftButton == ButtonState.Pressed && prevMouse.LeftButton == ButtonState.Released);
}
public bool CheckKeyboard(Keys key)
{
return (keyboard.IsKeyDown(key) && prevKeyboard.IsKeyDown(key));
}
public void Draw(SpriteBatch spriteBatch)
{
Color color;
int linePadding = 3;
spriteBatch.Begin();
mBatch.Begin();
mBatch.Draw(mTheQuantumBros2, new Rectangle(300, 150, mTheQuantumBros2.Width, mTheQuantumBros2.Height), Color.White);
if (gameTime.TotalGameTime.TotalSeconds <= 3)
{
mBatch.End();
for (int i = 0; i < buttonList.Count; i++)
{
color = (i == selected) ? Color.LawnGreen : Color.Gold;
spriteBatch.DrawString(spriteFont, buttonList[i], new Vector2((Game1.screen.Width / 2) - (spriteFont.MeasureString(buttonList[i]).X / 2), (Game1.screen.Height / 2) - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i)), color);
}
spriteBatch.End();
}
}
}
}