游戏对象不在 MonoGame 中保存状态

Game object not saving state in MonoGame

在我游戏的游戏逻辑部分,您检查输入的地方,为什么我的对象之前的状态没有被用于进一步评估我的功能?

我的游戏class:

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using System.Diagnostics;

namespace MonoRPG
{
    public class Game1 : Game
    {

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;


        public Game1()
            : base()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        protected override void UnloadContent()
        {
        }


        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            Player playerObject = new Player(this, this.Content, this.spriteBatch);
            KeyboardState oldState;
            KeyboardState newState = Keyboard.GetState();

            if (newState.IsKeyDown(Keys.Right))
            {
                Debug.WriteLine("right key pressed");
            }

            oldState = newState;


            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            renderMap createMap = new renderMap(this, this.Content, this.spriteBatch);
            Player playerObject = new Player(this, this.Content, this.spriteBatch);

            createMap.RenderMap();
            playerObject.drawPlayer();
            playerObject.positionX = playerObject.positionX + 10;

            base.Draw(gameTime);
        }
    }
}

还有我的玩家class:

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using System.Diagnostics;

namespace MonoRPG
{
    public class Player
    {
        public int positionX = 0;
        public int positionY = 0;

        Game1 draw = new Game1();
        ContentManager gameContent;
        SpriteBatch playerSprites;
        KeyboardState oldState;


        public Player(Game1 canvas, ContentManager content, SpriteBatch spriteBatch)
        {
            draw = canvas;
            gameContent = content;
            playerSprites = spriteBatch;
        }

        public Player()
        {

        }


        public void drawPlayer()
        {
            Texture2D playerTexture = gameContent.Load<Texture2D>("player/player.png");

            playerSprites.Begin();
            playerSprites.Draw(playerTexture, new Vector2(positionX, positionY), Color.Red);
            playerSprites.End();
        }

        public void playerMove(KeyboardState keyState, KeyboardState oldState)
        {
            positionX = positionX + 1;
        }

    }
}

绘图工作正常,但我用于播放器的矩形精灵的位置不会改变。问题与我创建对象的方式有关吗?我尝试在函数外声明,但后来我不能使用 this 关键字。如何调用现有对象的函数?

您应该在 InitializeLoadContent 方法中初始化(创建)对象。该对象应该只创建一次,而不是游戏的每次更新。 InitializeLoadContent 仅在游戏启动时发生一次(首先是 Initialize,当您调用 base.Initialize()LoadContent)。

当您将代码放入 Update 方法时,它会在游戏的每次更新(通常是每一帧)时执行。您的代码应该更像这样

Player player;
KeyboardState oldState; // player and oldState belongs to the whole game

protected override void Initialize() {
    // remember that spriteBatch is still null here
    player = new Player(this, Content, spriteBatch);
    // the line below initializes the spriteBatch
    // check the comments to see why this exact code won't work
    base.Initialize();
}

protected override void Update(GameTime gameTime) {
    KeyboardState newState = Keyboard.GetState(); // newState belongs to this exact update only

    if (newState.IsKeyDown(Keys.Right) && oldState.IsKeyUp(Keys.Right)) {
        player.playerMove(newState, oldState); // not sure why you wish to pass these arguments?
    }

    oldState = newState;
}

protected override void Draw(GameTime gameTime) {
    GraphicsDevice.Clear(Color.CornflowerBlue);

    renderMap createMap = new renderMap(this, this.Content, this.spriteBatch);

    createMap.RenderMap();

    // the player here is the same player in the Update method
    player.drawPlayer();

    base.Draw(gameTime);
}