2014-06-13 22 views
0

我已經對包括此站點在內的互聯網進行了一些研究,但我仍然對如何處理此錯誤感到困惑。我有兩個類,我使用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; 
     } 
    } 

回答

1

靜態方法可以被稱爲一個類,而非靜態方法需要在物體上被調用。你正試圖在一個類上調用一個非靜態方法;

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"); 
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"); 
} 

您應該查看更多關於靜態方法和非靜態方法的信息,看看有什麼不同。

相關問題