我已经在互联网上做了一些研究,包括这个网站,但我仍然对如何处理这个错误感到困惑。我有2个类,我使用的是MainSkeleton,它是Game1类,它有移动方法,同时我还创建了一个处理用户输入的类InputSkeleton。当我尝试调用移动方法时,即使我调用了该类,我也会收到错误。
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;
}
}
答案 0 :(得分:1)
可以在类上调用 static 方法,而需要在对象上调用非静态方法。你试图在类上调用非静态方法;
MainSkeleton.Move("up");
您需要将Move()
方法设置为静态方法(在这种情况下不是一个选项,因为您希望每次都在同一个对象上调用该方法),或者您需要以某种方式进入原始MainSkeleton
对象并在其上调用方法。
获取原始对象的一种简单方法是将其传递给您从MainSkeleton
调用的InputObject构造函数;
input = new InputSkeleton(1, this); // Pass in myself
...并在构造函数中获取对象;
MainSkeleton mainSkeleton;
public InputSkeleton(int p, MainSkeleton m)
{
player = p;
mainSkeleton = m;
}
...最后在对象上调用move()
;
mainSkeleton.Move("up");
答案 1 :(得分:0)
问题是你在没有创建实例的情况下调用类。 您需要首先创建一个实例来调用类中的非静态方法。 实施例;
private MainSkeleton _MainSkeleton = new MainSkeleton();
private void Update()
{
// The below is calling a method and telling what object is calling that method.
_MainSkeleton.Move("right");
// The below calls a method but does not link to any object. The below is only able to call non-static methods.
MainSkeleton.Move("right");
}
您应该查看有关静态与非静态方法的更多信息,以了解其中的区别。