我遇到了以下问题:在我的代码中,只打印了一张图片,矩形rectPlayer
在Initialize()
中有正确的值,但在Draw()
的底部所有值都是0.有人可以向我解释原因吗?
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
using System.Linq;
namespace StateMachine
{
internal class GameLoop
{
private Rectangle rectPlayer;
private int playerPositionX = 800;
private int playerPositionY = 900;
protected void Initialize()
{
rectPlayer = new Rectangle(playerPositionX, playerPositionY, 40, 65);
}
public GameState Update(GameTime gameTime)
{
return GameState.GameLoop;
}
public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Texture2D playerSprite)
{
spriteBatch.Draw(playerSprite, rectPlayer, Color.White);
}
}
}
这是调用类:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
sing System;
using System.Collections.Generic;
using System.Linq;
namespace StateMachine
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class StateMachine : Microsoft.Xna.Framework.Game
{
public static GraphicsDeviceManager graphics;
private SpriteBatch spriteBatch;
private SpriteFont font;
private GameState currentGameState, previousGameState, targetGameState;
public Color Background = Color.Black;
private SplashScreen splashScreen;
private MainMenu mainMenu;
private GameLoop gameLoop;
private Options options;
private Pause pause;
private GameOver gameOver;
private Credits credits;
private Quit quit;
private Texture2D playerSprite;
private Texture2D splashScreenPicture;
public GameState gameState
{
get { return currentGameState; }
}
public void ExitCode()
{
this.Exit();
}
public StateMachine()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.IsFullScreen = false;
graphics.PreferredBackBufferWidth = 1920;
graphics.PreferredBackBufferHeight = 1080;
graphics.ApplyChanges();
}
protected override void Initialize()
{
IsMouseVisible = true;
currentGameState = GameState.GameLoop;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
splashScreen = new SplashScreen();
mainMenu = new MainMenu();
gameLoop = new GameLoop();
options = new Options();
pause = new Pause();
gameOver = new GameOver();
credits = new Credits();
quit = new Quit();
font = Content.Load<SpriteFont>("font");
playerSprite = Content.Load<Texture2D>("PanzerknackerR1");
splashScreenPicture = Content.Load<Texture2D>("SplashPic");
}
protected override void UnloadContent()
{
}
protected void Input(GameTime gameTime)
{
InputManager.Update();
}
/// <summary>
/// Main Update methode. Uses the current Gamestate.Update
/// </summary>
/// <param name="gameTime"></param>
protected override void Update(GameTime gameTime)
{
Input(gameTime);
if (InputManager.GamePadButtonPress(Buttons.Back) || InputManager.KeyPress(Keys.Escape))
this.Exit();
switch (currentGameState)
{
case GameState.SplashScreen:
targetGameState = splashScreen.Update(gameTime, Content);
break;
case GameState.MainMenu:
targetGameState = mainMenu.Update(gameTime);
break;
case GameState.GameLoop:
targetGameState = gameLoop.Update(gameTime);
break;
case GameState.Options:
targetGameState = options.Update(gameTime);
break;
case GameState.Pause:
targetGameState = pause.Update(gameTime);
break;
case GameState.GameOver:
targetGameState = gameOver.Update(gameTime);
break;
case GameState.Credits:
targetGameState = credits.Update(gameTime);
break;
case GameState.Quit:
targetGameState = quit.QuitUpdate(gameTime, this);
break;
}
previousGameState = currentGameState;
currentGameState = targetGameState;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
// GraphicsDevice.Clear(Color.Black);
switch (currentGameState)
{
case GameState.SplashScreen:
splashScreen.Draw(gameTime, spriteBatch, splashScreenPicture);
break;
case GameState.MainMenu:
mainMenu.Draw(gameTime);
break;
case GameState.GameLoop:
gameLoop.Draw(gameTime, spriteBatch, playerSprite);
break;
case GameState.Options:
options.Draw(gameTime);
break;
case GameState.Pause:
pause.Draw(gameTime);
break;
case GameState.GameOver:
gameOver.Draw(gameTime);
break;
case GameState.Credits:
credits.Draw(gameTime);
break;
case GameState.Quit:
quit.QuitDraw(gameTime);
break;
default:
spriteBatch.DrawString(font, "ERROR: GameState", new Vector2(100, 100), Color.White);
break;
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}