Player movement is snappy

for example, when W A S D are pressed in quick succession and repeatedly, it sometimes lags opposite to the direction it’s going.

Vini vini;

void setup() {
 fullScreen();
 vini = new Vini();
}

void draw() {
  background(50);
  vini.display();
  vini.movement();
}

void keyPressed() {
 vini.keys[key] = true; 
}

void keyReleased() {
 vini.keys[key] = false; 
}

class Vini {

  float x = width/2;
  float y = height/2;
  float velx;
  float vely;
  boolean [] keys = new boolean[255];

  void display() {
    ellipse(x, y, 50, 50);
  }

  void movement() {

    if (keys['w']) {
      y += vely;
      vely = -10;
    }
    if (keys['s']) {
      y += vely;
      vely = 10;
    }
    if (keys['a']) {
      x += velx;
      velx = -10;
    }
    if (keys['d']) {
      x += velx;
      velx = 10;
    }
  }
}

thanks for helping! I’m only learning how to do stuff, it’s not some serious project.

Hi

Programming Movement with Keyboard Controls in Processing

Hi @vinijoncrafts,

Why you are setting your velocity after you are using it ?
Guess you want to set it beforehand.
As the values are constant, I would recommend to set a constant speed in the constructor an add/subtract in the movement method accordingly…

Cheers
— mnse

2 Likes

OOOOOOH I thought the order didn’t matter at all! so it would be like this?

if (keys['z']) {
velz = 10;
z += 10;
}

thanks a lot!!! Also, the speed will not be constant, that’s why I am changing the speed instead of the method.

Hi @vinijoncrafts,

It is important… :slight_smile:

Sequence

Sequence is the first programming construct. In programming, statements are executed one after another. Sequence is the order in which the statements are executed.

The sequence of a program is extremely important as carrying out instructions in the wrong order leads to a program performing incorrectly.

Cheers
— mnse

The three basic programming constructs - Programming fundamentals - OCR - GCSE Computer Science Revision - OCR - BBC Bitesize.

1 Like