返回一个错误。
CS0029
C# 无法隐式将类型 'void' 转换为 'System.EventHandler'
该函数被用在这里:
gameTimer.Tick += UpdateScreen();
该函数的作用是:
private void UpdateScreen()
{
if(Settings.GameOver == true)
{
if (Input.KeyPressed(Keys.Enter))
{
StartGame();
}
}
else
{
if (Input.KeyPressed(Keys.Right) && Settings.direction != Direction.Left)
Settings.direction = Direction.Right;
else if (Input.KeyPressed(Keys.Left) && Settings.direction != Direction.Right)
Settings.direction = Direction.Left;
else if (Input.KeyPressed(Keys.Up) && Settings.direction != Direction.Down)
Settings.direction = Direction.Up;
else if (Input.KeyPressed(Keys.Down) && Settings.direction != Direction.Up)
Settings.direction = Direction.Down;
MovePlayer();
}
pbCanvas.Invalidate();
}
UpdateScreen
是方法本身时,执行UpdateScreen()
;正确的语法是gameTimer.Tick += UpdateScreen;
。我们将Tick
分配给该方法,而不是方法的结果(即void
)。 - Dmitry Bychenko