0

I am trying to move an image which represents the players avatar using the arrow key in WPF. The code should allow the player to move the image using the arrow keys however this is not working. This is the code I have written to move the image.

private void Canvas_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Down)
            {
                Canvas.SetTop(player, Canvas.GetTop(player) -10);
            }
            else if (e.Key == Key.Up)
            {
                Canvas.SetTop(player, Canvas.GetTop(player) - 10);
            }
            else if (e.Key == Key.Left)
            {
                Canvas.SetLeft(player, Canvas.GetLeft(player) - 10);
            }
            else if (e.Key == Key.Right)
            {
                Canvas.SetLeft(player, Canvas.GetLeft(player) + 10);
            }

        }
Jay s
  • 9
  • 3

1 Answers1

1

First the disclaimer: Desktop Technologies like Windwos Forms and WPF are not suiteable for game development. Turnbased, singleplayer or hotseat multiplayer is about the best you can do. For Windows Forms the upper limit is the old Solitair game. WPF is a bit better, but still not a good choice.

For proper development, you need something with a Game Loop. In .NET you got the dated XNA and these current chioces.

Back to your problem however: Asuming you registered that event with the Canvas, it will only trigger the event if it ha the input focus right then. Something you will not be able to maintain.

There are stuff like routing and tunneling events, but I think what you really want is a Hotkey. However examples seem to asume you use the Command Pattern, and then add Hotkey options to it:

Christopher
  • 9,634
  • 2
  • 17
  • 31