Search code examples
c#xna

XNA An object reference is required for the non-static field, method, or property error


I've done some research around the internet including this site but I am still confused about what to do with this error. I have 2 classes I'm using MainSkeleton which is the Game1 class and it has the move method while I also made a class InputSkeleton that handles user inputs. When I try calling the move method I'm getting the error even though I called the class.

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

Solution

  • A static method can be called on a class, while a non static method needs to be called on an object. You're trying to call a non static method on a class;

    MainSkeleton.Move("up");
    

    Either you need to make the Move() method a static method (which in this case is not an option since you want to call the method on the same object every time), or you need to somehow get to the original MainSkeleton object and call the method on that.

    A simple way to get to the original object is to pass it in to the InputObject constructor which you call from MainSkeleton;

    input = new InputSkeleton(1, this);   // Pass in myself
    

    ...and take the object in the constructor;

    MainSkeleton mainSkeleton;
    
    public InputSkeleton(int p, MainSkeleton m)
    {
        player = p;
        mainSkeleton = m;
    }
    

    ...and finally call move() on the object instead;

    mainSkeleton.Move("up");