我已經對包括此站點在內的互聯網進行了一些研究,但我仍然對如何處理此錯誤感到困惑。我有兩個類,我使用MainSkeleton,它是Game1類,它有移動方法,同時我還創建了一個處理用戶輸入的類InputSkeleton。當我嘗試調用移動方法時,即使我調用該類,我也會收到錯誤。XNA非靜態字段,方法或屬性錯誤需要對象引用
MainSkeleton
/// <summary>
/// This is the main type for your game
/// </summary>
public class MainSkeleton: Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
InputSkeleton input;
SpriteBatch spriteBatch;
Color backColor = Color.CornflowerBlue;
public MainSkeleton()
{
input = new InputSkeleton(1);
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// 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.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
// This is a texture we can render.
Texture2D myTexture;
// Set the coordinates to draw the sprite at.
Vector2 spritePosition = Vector2.Zero;
// Store some information about the sprite's motion.
Vector2 spriteSpeed = new Vector2(50.0f, 50.0f);
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
myTexture = Content.Load<Texture2D>("Person");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
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
input.Update();
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// Draw the sprite.
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(myTexture, spritePosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
public void Move(String direction)
{
int MaxX = graphics.GraphicsDevice.Viewport.Width - myTexture.Width;
int MinX = 0;
int MaxY = graphics.GraphicsDevice.Viewport.Height - myTexture.Height;
int MinY = 0;
if (direction.Equals("up"))
{
if (spritePosition.Y > MinY)
{
spritePosition.Y = spritePosition.Y - 1;
}
else
{
spritePosition.Y = MinY;
}
}
if (direction.Equals("down"))
{
if (spritePosition.Y < MaxY)
{
spritePosition.Y = spritePosition.Y + 1;
}
else
{
spritePosition.Y = MaxY;
}
}
if (direction.Equals("left"))
{
if (spritePosition.X > MinX)
{
spritePosition.X = spritePosition.X - 1;
}
else
{
spritePosition.X = MinX;
}
}
if (direction.Equals("right"))
{
if (spritePosition.X < MaxX)
{
spritePosition.X = spritePosition.X + 1;
}
else
{
spritePosition.X = MinX;
}
}
}
}
InputSkeleton
public class InputSkeleton
{
public KeyboardState newKState;
public KeyboardState oldKState;
public MouseState newMState;
public MouseState oldMState;
public Boolean menuState;
public Boolean gameRun;
public int player;
public InputSkeleton(int p)
{
player = p;
}
public void Update()
{
if (menuState == true && gameRun == false)
{
MenuInput();
}
else if (menuState == false && gameRun == true)
{
PlayerInput();
}
}
public void MenuInput()
{
}
public void PlayerInput()
{
newKState = Keyboard.GetState();
if (newKState.IsKeyDown(Keys.Up))
{
MainSkeleton.Move("up");
}
if (newKState.IsKeyDown(Keys.Down))
{
MainSkeleton.Move("down");
}
if (newKState.IsKeyDown(Keys.Left))
{
MainSkeleton.Move("left");
}
if (newKState.IsKeyDown(Keys.Right))
{
MainSkeleton.Move("right");
}
oldKState = newKState;
}
}