XNA(MonoGame)零异常
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XNA(MonoGame)零异常相关的知识,希望对你有一定的参考价值。
我试图绘制屏幕我的播放器和一些实体,但我的播放器的更新方法我得到null异常...
我的SpriteManager
public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
{
player.Update(gameTime, Game.Window.ClientBounds);
foreach(Sprite s in spriteList)
{
s.Update(gameTime, Game.Window.ClientBounds);
if(s.collisionRect.Intersects(player.collisionRect))
Game.Exit();
}
base.Update(gameTime);
}
和我的玩家类这就是我处理输入的方式
public override Vector2 direction {
get {
Vector2 inputDirection = Vector2.Zero;
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Left))
inputDirection.X -= 1;
if (keyboardState.IsKeyDown(Keys.Right))
inputDirection.X += 1;
if (keyboardState.IsKeyDown(Keys.Up))
inputDirection.Y -= 1;
if (keyboardState.IsKeyDown(Keys.Down))
inputDirection.Y += 1;
return inputDirection * speed;
}
}
这是导致null异常的更新
public override void Update(GameTime gameTime, Rectangle clientBounds) {
position += direction;
if(position.Y < 0)
position.Y = 0;
if(position.X < 0)
position.X = 0;
if(position.Y > clientBounds.Height - frameSize.Y)
position.Y = clientBounds.Height - frameSize.Y;
if(position.X > clientBounds.Width - frameSize.X)
position.X = clientBounds.Width - frameSize.X;
base.Update(gameTime, clientBounds);
}
答案
我注意到的第一件事是需要标记:
public override void Update(GameTime gameTime, Rectangle clientBounds) {
position += direction;
if(position.Y < 0)
position.Y = 0;
if(position.X < 0)
position.X = 0;
if(position.Y > clientBounds.Height - frameSize.Y)
position.Y = clientBounds.Height - frameSize.Y;
if(position.X > clientBounds.Width - frameSize.X)
position.X = clientBounds.Width - frameSize.X;
base.Update(gameTime, clientBounds);
}
我注意到的第二件事是你的玩家类返回:inputDirection * speed;
但是没有变量设置为speed
接受该返回。
以上是关于XNA(MonoGame)零异常的主要内容,如果未能解决你的问题,请参考以下文章