我正在尝试绘制我的播放器和某些实体的屏幕,但是我的播放器更新方法会出现空例外...
我的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;
}
}
这是导致空例外的更新
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);
}
答案 0 :(得分:0)
我注意到的第一件事是需要标签:
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
来接受该返回。