0

I'm trying to move a rectangular over the screen with WASD. My problem is that when the user tries to change the direction of the paddle twice too fast (let's say you press left, then right and left again super fast), the object freezes for a split second, and continues to move along its direction.. I'm open for ideas on how to fix it. Thank you.

Global variables

int velocityX = 0;
int velocityY = 0;
Boolean flagA = false;
Boolean flagD = false;
Boolean flagW = false;
Boolean flagS = false;
Sprite player = new Sprite(100, 100, 60, 60, "player", Color.BLACK);
Thread t1;
Thread t2;
Thread t3;

Thread definitions for movement (seOnKeyPressed)

t1 = new Thread(new Runnable() {
              @Override
              public void run() {
                  while(true) {
                      scene.setOnKeyPressed(e -> {
                            switch(e.getCode()) {
                            case A:
                                flagA = true;
                                player.velocityX = -1;
                                break;
                            case D:
                                flagD = true;
                                player.velocityX = 1;
                                break;
                            case W:
                                flagW = true;
                                player.velocityY = -1;
                                break;
                            case S:
                                flagS = true;
                                player.velocityY = 1;
                                break;
                            }
                });
                  }
              }
           });

Thread definition (setOnKeyReleased)

t2 = new Thread(new Runnable() {
              @Override
              public void run() {
                  while(true) {
                      scene.setOnKeyReleased(e -> {
                            switch(e.getCode()) {
                            case A:
                                flagA = false;
                                player.velocityX = 0;
                                break;
                            case D:
                                flagD = false;
                                player.velocityX = 0;
                                break;
                            case W:
                                flagW = false;
                                player.velocityY = 0;
                                break;
                            case S:
                                flagS = false;
                                player.velocityY = 0;
                                break;
                            }
                });
                  }

              }
           });

AnimationTimer for updating object's location on the screen

AnimationTimer timer = new AnimationTimer() {

        @Override
        public void handle(long now) {
            player.setTranslateX(player.getTranslateX() + player.velocityX);
            player.setTranslateY(player.getTranslateY() + player.velocityY);
        }

    };

Starting the program

//Starting threads
   t1.start();
   t2.start();

//AnimationTimer for updating X & Y location
   timer.start();
emir3333
  • 7
  • 4
  • First, don't use multiple threads for this, it's a bad idea; Second, you should have some kind of `List` which contains the active set of key strokes (adding and removing them as they are activated); Third, you should have ONE "main" loop which is responsible for updating the state of the model and scheduling paint updates. Since you're using JavaFX, I would suggest the `AnimationTimer` is the perfect place for this – MadProgrammer Nov 22 '18 at 23:26
  • Also, doing `scene.setOnKeyReleased(e -> {` inside your loops is adding ANOTHER observer on each loop, which is going to greatly degrade the performance over a very short period of time. You're coding in a "event driven environment", this means, something happens and you respond to it (hence the observers), you don't need a "loop" to manage this, set it once and leave it alone – MadProgrammer Nov 22 '18 at 23:28
  • Dude thank you so much for the suggestions. I've been busting my head since yesterday and when I applied what you've said, it instantly started working. I didn't implement the List though, since it's the same thing with key pressed & released flags. Thank you dude. – emir3333 Nov 23 '18 at 11:04

0 Answers0