【发布时间】:2021-03-04 04:56:32
【问题描述】:
我正在创建一个根据按下的键在屏幕上移动精灵的方法
class InputController
{
public void DetectInput (Vector2 position)
{
if (Keyboard.GetState().IsKeyDown(Keys.W))
{
position.Y -= 1;
}
if (Keyboard.GetState().IsKeyDown(Keys.S))
{
position.Y += 1;
}
if (Keyboard.GetState().IsKeyDown(Keys.A))
{
position.X -= 1;
}
if (Keyboard.GetState().IsKeyDown(Keys.D))
{
position.X += 1;
}
如果我在其他班级尝试调用,但收到错误消息:
System.NullReferenceException: '对象引用未设置为对象的实例
InputController 为空
这是其他类的相关代码
private InputController InputController;
protected override void Update(GameTime gameTime)
{
InputController.DetectInput(_position);
}
【问题讨论】: