Movement script continues moving diagonally

So I’m working on a movement script for a game, but when I go from diagonal to just one direction, it continues moving diagonally.

PImage pS[]; //Allocating a spot for Player sprites
Player p; //Creates the player

//Keyboard inputs
boolean up;
boolean down;
boolean left;
boolean right;
boolean run;
//End keyboard inputs

void setup() {
  size(1024, 896);
  background(0);
  p = new Player();
  up = false;
  down = false;
  left = false;
  right = false;
}

void draw() {
  background(0);
  p.move();
  p.show();
}

class Player {
  PVector pos;
  PVector vel;
  PVector acc;
  int max;
  Player() {
    pos = new PVector(0, 192);
    vel = new PVector();
    acc = new PVector();
    max = 3;
  }
  void show() {
    rect(pos.x,pos.y,40,40);
  }
  void move() {
    if (up||down||left||right) {
    if (up) {
      acc.y = -0.1;
    } else if (down) {
      acc.y = 0.1;
    }
    if (left) {
      acc.x = -0.1;
    } else if (right) {
      acc.x = 0.1;
    }
    if (run) {
      max=5;
    } else {
      max=3;
    }
    vel = PVector.add(acc,vel);
    } else {
      acc.x = 0;
      acc.y = 0;
      vel = PVector.div(vel, 1.1);
    }
    vel.limit(max);
    pos = PVector.add(pos,vel);
  }
}

class Camera {
  
}

void keyPressed() {
  if (key=='w') {
    up=true;
  } else if (key=='s') {
    down=true;
  }
  if (key=='a') {
    left=true;
  } else if (key=='d') {
    right=true;
  }
  if (key==CODED) {
    if (keyCode==SHIFT) {
      run=true;
    }
  }
}

void keyReleased() {
  if (key=='w') {
    up=false;
  } else if (key=='s') {
    down=false;
  }
  if (key=='a') {
    left=false;
  } else if (key=='d') {
    right=false;
  }
  if (key==CODED) {
    if (keyCode==SHIFT) {
      run=false;
    }
  }
}

If there’s a better way to do this, let me know.

try my RUNNER code version

int[]     keysb={0,0,0,0};                     // pressed key memory, NOW INT ( as speed )
char[]    keyss={'w','s','a','d'};             // key list to be detected parallel UP DOWN LEFT RIGHT
int x, y, speed = 3, size = 10;

void setup() {
  size(400, 400);
  print_keyinfo();
}

void draw() {
  background(255);
  move();
}

void print_keyinfo() {
  print("use keys:");
  for ( int i=0;i<4;i++) print("["+keyss[i]+"] ");
  println("\nfor      UP DOWN LEFT RIGHT");
}

void move() {
    x = constrain(x += keysb[3]-keysb[2] , 0, width-size-1);
    y = constrain(y += keysb[1]-keysb[0] , 0, height-size-1);
    square(x, y, size);
}

void keyPressed() {
  for ( int i=0;i<4;i++) if ( key==keyss[i] ) keysb[i]=speed;
}

void keyReleased() {
  for ( int i=0;i<4;i++) if ( key==keyss[i] ) keysb[i]=0;
}

responds well also diagonal

OR

// 2 player / hand mode for 2 ( diagonal ) runner 

int[]     p1keysb={0,0,0,0};                     // pressed key memory, NOW INT SPEED
char[]    p1keyss={'w','s','a','d'};             // key list to be detected parallel UP DOWN LEFT RIGHT
int[]     p2keysb={0,0,0,0};                     // pressed key memory, NOW INT SPEED
char[]    p2keyss={38,40,37,39};                 // key CODE to be detected parallel UP DOWN LEFT RIGHT
int x1=0, y1, x2=0, y2, speed = 3, size = 10;

void setup() {
  size(400, 400);
  x2 = width;
  noStroke();
  print_keyinfo();
}

void draw() {
  background(255);
  move();
}

void print_keyinfo() {
  print("Player 1 use keys:");
  for ( int i=0;i<4;i++) print("["+p1keyss[i]+"] ");
  print("\nPlayer 2 use keys:");
  print("[ Arrow Keys ] ");
  println("\nfor      UP DOWN LEFT RIGHT");
}

void move() {                           // and draw
    x1 = constrain(x1 += p1keysb[3]-p1keysb[2] , 0, width-size-1);
    y1 = constrain(y1 += p1keysb[1]-p1keysb[0] , 0, height-size-1);
    fill(200,0,0);
    square(x1, y1, size);
    x2 = constrain(x2 += p2keysb[3]-p2keysb[2] , 0, width-size-1);
    y2 = constrain(y2 += p2keysb[1]-p2keysb[0] , 0, height-size-1);
    fill(0,0,200);
    square(x2, y2, size);
}

void keyPressed() {
  //println("key "+key+" keyCode "+keyCode);
  for ( int i=0;i<4;i++) if ( key    ==p1keyss[i] ) p1keysb[i]=speed;
  for ( int i=0;i<4;i++) if ( keyCode==p2keyss[i] ) p2keysb[i]=speed;
}

void keyReleased() {
  for ( int i=0;i<4;i++) if ( key    ==p1keyss[i] ) p1keysb[i]=0;
  for ( int i=0;i<4;i++) if ( keyCode==p2keyss[i] ) p2keysb[i]=0;
}

this keyCode code might depend on OS.

The code does work well, but it doesn’t have velocity. I was using vectors because of how useful the functions were for my movement system. I’ll try to implement some of your control code though

You should get rid of this, as you test for those keys inside the move() block anyways.

Thanks for the help! Removing the if & else statement fixed my issue.

No problem, brine. :grin: