I am trying to make a mini game with w,a,s,d as forward, left, down and right respectively.
The control moves up, left, down and right but only once. If i press the ‘W’,’S’,’A’ or ‘D’ key twice or thrice the control only moves up once.
This is the code i have currently for moving the control right:
private void moveRight(Button button)
{
// Gets the current position of the control
Point relativePoint = button.TransformToAncestor(parentCanvas).Transform(new Point(0, 0));
// Reason for having relative - relative is that its adding onto the previous point.
Canvas.SetLeft(button, relativePoint.X - relativePoint.X + 5);
button.Content = relativePoint;
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.W)
{
moveUp(button1);
}
else if (e.Key == Key.A)
{
moveLeft(button1);
}
else if (e.Key == Key.S)
{
moveDown(button1);
}
else if (e.Key == Key.D)
{
moveRight(button1);
}
}
Please help or advise me how to go about solving this problem.
All help will greatly be appreciated.
Thanks
Figured it out, the logic was a bit out.
The correct way to do it is like this:
Its working now, Thanks