# Movement script continues moving diagonally

**URL:** <https://discourse.processing.org/t/movement-script-continues-moving-diagonally/15854>\
**Category:** Coding Questions\
**Created:** [November 27, 2019, 4:15am UTC](https://discourse.processing.org/t/movement-script-continues-moving-diagonally/15854 "2019-11-27T04:15:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![madbrine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/madbrine/32/7271_2.png) [@madbrine](https://discourse.processing.org/u/madbrine)\
**Post date:** [November 27, 2019, 4:15am UTC](https://discourse.processing.org/t/movement-script-continues-moving-diagonally/15854/1 "2019-11-27T04:15:27Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 27, 2019, 4:52am UTC](https://discourse.processing.org/t/movement-script-continues-moving-diagonally/15854/2 "2019-11-27T04:52:03Z")

</div>

try my RUNNER code version

```auto
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

```auto
// 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.

---

<div class="post-metadata">

**Author:** ![madbrine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/madbrine/32/7271_2.png) [@madbrine](https://discourse.processing.org/u/madbrine)\
**Post date:** [November 27, 2019, 5:07am UTC](https://discourse.processing.org/t/movement-script-continues-moving-diagonally/15854/3 "2019-11-27T05:07:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unique\_username](https://avatars.discourse-cdn.com/v4/letter/u/a88e57/32.png) [@unique\_username](https://discourse.processing.org/u/unique_username)\
**Post date:** [November 27, 2019, 9:30pm UTC](https://discourse.processing.org/t/movement-script-continues-moving-diagonally/15854/4 "2019-11-27T21:30:45Z")

</div>

> [@madbrine](#):
>
> if (up||down||left||right) {

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

---

<div class="post-metadata">

**Author:** ![madbrine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/madbrine/32/7271_2.png) [@madbrine](https://discourse.processing.org/u/madbrine)\
**Post date:** [November 28, 2019, 7:24pm UTC](https://discourse.processing.org/t/movement-script-continues-moving-diagonally/15854/5 "2019-11-28T19:24:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unique\_username](https://avatars.discourse-cdn.com/v4/letter/u/a88e57/32.png) [@unique\_username](https://discourse.processing.org/u/unique_username)\
**Post date:** [November 28, 2019, 7:26pm UTC](https://discourse.processing.org/t/movement-script-continues-moving-diagonally/15854/6 "2019-11-28T19:26:27Z")

</div>

No problem, brine. 😁
