Loading...
Logo
Processing Forum

Movement problem

in Programming Questions  •  5 months ago  
When I move an object using the arrow keys it lags for a bit some times, e.g. if the object was moving left for a few sec and I click down arrow key, it will move the dirrectionOffset in a downard position once (move down by 3 px in this case), then lag for a while and then continue moving down ( 3px each  time).

Any idea what could be causing this ?

  void move() {
    if (keyPressed==true) {
      if (key==CODED) {
        if (keyCode==UP) {
          dy = -directionOffset;
        }
        else if (keyCode==DOWN) {
          dy = directionOffset;
        }
        else if (keyCode==LEFT) {
          dx = -directionOffset;
        }
        else if (keyCode==RIGHT) {
          dx = directionOffset;
        }
      }
    }
    x = x + dx;
    dx = 0;
    y = y + dy;
    dy = 0;
  }

Replies(5)

Re: Moment problem

5 months ago
I don't know what directionoffset stand for…but I think this does the job:
Copy code
  1. float x, y, dx, dy, directionOffset;
    void setup()
    {
      size(500, 500);
    }
    void draw()
    {
      background(200);
      move();
      ellipse(x, y, 20, 20);
    }


    void move() {
      if (keyPressed==true) {
        if (key==CODED) {
          if (keyCode==UP) {
            dy--;
          }
          else if (keyCode==DOWN) {
            dy++;
          }
          else if (keyCode==LEFT) {
            dx--;
          }
          else if (keyCode==RIGHT) {
            dx++;
          }
        }
      }
      x = x + dx;
      dx = 0;
      y = y + dy;
      dy = 0;
    }

Re: Moment problem

5 months ago
dirrectionOffset is basically the ++ -- things, it just I needed to increment more than 1px a time so I declared it outside of the method. Its value is   = 3. So it increments each time by 3px instead of 1.
So that isn't the problem :/

Re: Movement problem

5 months ago
Perhaps it is an issue with your keyboard buffer: the auto-repeat feature of your keyboard might be fast, so you have a few keystrokes stored in the buffer that needs to be processed before the new key can be.
FYI, users reported differences of behavior of keyboard depending on the system (Windows / Linux / Mac).

Re: Movement problem

5 months ago
Is there a way to fix this ? Or to disable the buffer? just go from pure input without storing any previous keys.

Re: Movement problem

5 months ago
maybe use void keyReleased() instead of keyPressed…