I want to move player on the panel by using arrows. My panel has a Paint event:
private void panel1_Paint(object sender, PaintEventArgs e)
{
var gameManager = new GameManager(this, e.Graphics);
}
GameManager.cs
public GameManager(Form1 gameForm, Graphics graphic)
{
this.gameForm = gameForm;
this.graphic = graphic;
player = new Player(100, 100, graphic);
gameForm.KeyUp += MovePlayer; // Event to handle KeyUpevent
}
private void MovePlayer(object sender, System.Windows.Forms.KeyEventArgs e)
{
int x = 0, y = 0;
switch (e.KeyCode)
{
case System.Windows.Forms.Keys.Down:
y = 10;
player.Move(x, y);
break;
}
}
}
Player.cs
private SolidBrush brush = new SolidBrush(Color.Black);
public Player(int x, int y, Graphics graphic)
{
location = new Point(x, y);
this.graphic = graphic;
graphic.FillRectangle(brush, location.X, location.Y, 10, 10);
}
public void Move(int x, int y)
{
location.X += x;
location.Y += y;
graphic.FillRectangle(brush, location.X, location.Y, 10, 10); // Here I get an error
}
The problem: Player is generating correctly on the Panel when the player object is called (on the constructor). But when I click down arrow key
I get the following simply message:
System.ArgumentException: Parameter is invalid
But all the parameters seems to be valid, the SolidBrush object and the coordinates and dimensions. What is wrong?