在我的游戏的游戏逻辑部分,你检查输入,为什么我的对象之前的状态没有用于我的功能的进一步评估?
我的游戏课程:
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);
}
}
}
我的球员班:
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
关键字。如何调用现有对象上的函数?
答案 0 :(得分:1)
您应该使用Initialize
或LoadContent
方法初始化(创建)对象。该对象应该只创建一次,而不是每次更新游戏。 Initialize
和LoadContent
仅在游戏启动时发生一次(Initialize
优先,LoadContent
,当您致电base.Initialize()
时)。
当您将代码放入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);
}