2

So I am working on an game/app - 2D racing game and I am trying my best at it, but yet I have studied C# for like 1 year. My problem is that I have two images (simple cars) and I can move them with their location via KeyData. It works unless I want both of them to move at once. This is what I move it with =>

protected override bool Move(ref Message msg, Keys KeyData)
{
    if (KeyData == Keys.Up)
    {
        bluePoint.Y -= normalSpeed;
        Refresh();
        return true;
    }
    if (KeyData == Keys.Down)
    {
        bluePoint.Y += normalSpeed;
        Refresh();
        return true;
    }
    if (KeyData == Keys.Left)
    {
        bluePoint.X -= normalSpeed;
        Refresh();
        return true;
    }
    if (KeyData == Keys.Right)
    {
        bluePoint.X += normalSpeed;
        Refresh();
        return true;
    }

    if (KeyData == Keys.W)
    {
        redPoint.Y -= normalSpeed;
        Refresh();
        return true;
    }
    if (KeyData == Keys.S)
    {
        redPoint.Y += normalSpeed;
        Refresh();
        return true;
    }
    if (KeyData == Keys.A)
    {
        redPoint.X -= normalSpeed;
        Refresh();
        return true;
    }
    if (KeyData == Keys.D)
    {
        redPoint.X += normalSpeed;
        Refresh();
        return true;
    }

    return true;

}
Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
Julia P.
  • 35
  • 8
  • 1
    What framework are you using? Unity / WPF / WinForms .. can you update the tags? – Arcturus Apr 03 '18 at 09:25
  • Windows forms app – Julia P. Apr 03 '18 at 09:26
  • 1
    It seems a test in a college or something similar, the same question, with the same game 5 years ago....https://stackoverflow.com/questions/16745313/respond-to-multiple-keydown-events – amalbala Apr 03 '18 at 09:36
  • You mentioned that it works unless you want to move both images at once, what would result in both images actually needing to be moved at once? It seems as though each image has 4 keys which would move it in 4 different directions. Would there be a different key press to move both at the same time? – Danny Goodall Apr 03 '18 at 09:37
  • Anyway thanks, I will take a look of those – Julia P. Apr 03 '18 at 09:47

1 Answers1

1

Keys is a flags enum, so all the information is there, you probably should rewrite your if statements. Enum has a HasFlag method for your convenience:

if( keyData.HasFlag( Keys.Up ) )

Also you probably should not return true after handing one of your keys

More information about flags: What does the [Flags] Enum Attribute mean in C#?

protected override bool Move(ref Message msg, Keys KeyData)
{
    if (KeyData.HasFlag(Keys.Up))
    {
        bluePoint.Y -= normalSpeed;
    }
    if (KeyData.HasFlag(Keys.Down))
    {
        bluePoint.Y += normalSpeed;
    }
    if (KeyData.HasFlag(Keys.Left))
    {
        bluePoint.X -= normalSpeed;
    }
    if (KeyData.HasFlag(Keys.Right))
    {
        bluePoint.X += normalSpeed;
    }

    if (KeyData.HasFlag(Keys.W))
    {
        redPoint.Y -= normalSpeed;
    }
    if (KeyData.HasFlag(Keys.S))
    {
        redPoint.Y += normalSpeed;
    }
    if (KeyData.HasFlag(Keys.A))
    {
        redPoint.X -= normalSpeed;
    }
    if (KeyData.HasFlag(Keys.D))
    {
        redPoint.X += normalSpeed;
    }

    Refresh();
    return true;

}
Arcturus
  • 26,677
  • 10
  • 92
  • 107