2 events at once

I'm creating a simple, old, black and white tennis game for practice. I've created an event handler for 9 keys - 2 for moving 2 paddles up, 2 for moving them down, 2 for left and 2 for right. I also have an Enter key that starts the ball moving. The problem is that when I press Enter, none of my other keys work, until the ball goes off the screen. Does anyone have any suggestions how to fix this

Answer this question

2 events at once

  • wrreagle70

    Here is the code:

    private void Start_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)

    {

    //Down1

    if(e.KeyCode == Keys.S)

    {

    if(paddle3.Top < 315)

    {

    int y = paddle3.Top+3;

    paddle3.Top = y;

    }

    }

    //Up1

    if(e.KeyCode == Keys.W)

    {

    if(paddle3.Top > 0)

    {

    int y = paddle3.Top-3;

    paddle3.Top = y;

    }

    }

    //Left1

    if(e.KeyCode == Keys.A)

    {

    if(paddle3.Left > 0)

    {

    int x = paddle3.Left-3;

    paddle3.Left = x;

    }

    }

    //Right1

    if(e.KeyCode == Keys.D)

    {

    if(paddle3.Left < 235)

    {

    int x = paddle3.Left+3;

    paddle3.Left = x;

    }

    }

    //Down2

    if(e.KeyCode == Keys.K)

    {

    if(paddle2.Top < 315)

    {

    int y = paddle2.Top+3;

    paddle2.Top = y;

    }

    }

    //Up2

    if(e.KeyCode == Keys.I)

    {

    if(paddle2.Top > 0)

    {

    int y = paddle2.Top-3;

    paddle2.Top = y;

    }

    }

    //Left2

    if(e.KeyCode == Keys.J)

    {

    if(paddle2.Left > 350)

    {

    int x = paddle2.Left-3;

    paddle2.Left = x;

    }

    }

    //Right2

    if(e.KeyCode == Keys.L)

    {

    if(paddle2.Left < 575)

    {

    int x = paddle2.Left+3;

    paddle2.Left = x;

    }

    }

    if(e.KeyCode == Keys.Enter)

    {

    moveBall();

    }

    }


  • Jo&amp;#38;&amp;#35;227&amp;#59;o Leite

    Never mind about the white screen - I fixed that.
  • Brock Taylor

    hi,

    i'm not expert with animation but how do you update your paddle



  • UKGENT

    Another problem that I've run into, is that when the ball has been flying around for about 10-15 seconds, it stops, the program goes completely white, and the ball goes back to its starting position. Is this a problem wih the code, or the computer
  • AvalonBliss

    Thanks for all of the help from everyone, but I've already found the problem and now everything works perfectly.

    What happened, was that moveBal() was a function that moved the ball by 1 to 5 pixels in each direction. This happened in a 'for' cycle that only ended when the ball went off the screen. Thus, the call kept reoccuring, and none of my other handlers would work. I fixed this by impleminting a timer, which moved the ball with each tick of 5 milliseconds, and changed by Enter handler to start the timer.


  • SeonBong

    What does your moveBall() method do Sounds like it never returns...

  • Sheng Jiang (?晟)

    I tried using this.Focus() but it still doesn't work.
  • teyc

    If you provide mode detail, someone might be able to provide some guidance. I'll assume this is a WinForms application. What event are you subscribing to to handle the keys, KeyDown, KeyUp, or KeyPress What is the code in this event handler

  • Gouranga1

    I think when you click Enter and move the ball, the form lose its focus...


  • Moiss

    I change it's Top and Left properties
  • JohanBarnard

    I see that you have implemented all that on single thread .

    Try using multithreaded approach so as to keep your User interface responsive .

    Thanx



  • mepurathu

    hi,

    ok explain the solution and mark it as answer , it might help someone else

    best regards



  • 2 events at once