【问题标题】:XNA An object reference is required for the non-static field, method, or property errorXNA 非静态字段、方法或属性错误需要对象引用
【发布时间】:2014-06-13 04:14:05
【问题描述】:

我在互联网上做了一些研究,包括这个网站,但我仍然对如何处理这个错误感到困惑。我有 2 个类,我正在使用 MainSkeleton,它是 Game1 类,它具有 move 方法,同时我还创建了一个处理用户输入的 InputSkeleton 类。当我尝试调用 move 方法时,即使我调用了类,我也会收到错误。

主骨架

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

输入骨架

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

【问题讨论】:

    标签: c# xna


    【解决方案1】:

    静态方法可以在类上调用,而非静态方法需要在对象上调用。您正在尝试在类上调用非静态方法;

    MainSkeleton.Move("up");
    

    要么您需要将 Move() 方法设为静态方法(在这种情况下,这不是一个选项,因为您希望每次都在同一个对象上调用该方法),或者您需要以某种方式获取原始 @ 987654323@ 对象并在其上调用方法。

    获取原始对象的一种简单方法是将其传递给您从 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");
    

    【讨论】:

      【解决方案2】:

      问题是您在没有创建实例的情况下调用该类。 您需要首先创建一个实例来调用类中的非静态方法。 示例;

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

      您应该查看更多关于静态与非静态方法的信息,看看有什么区别。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多