# 8 directional movement in object

**URL:** <https://discourse.processing.org/t/8-directional-movement-in-object/40317>\
**Category:** Coding Questions\
**Created:** [December 26, 2022, 6:49pm UTC](https://discourse.processing.org/t/8-directional-movement-in-object/40317 "2022-12-26T18:49:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinijoncrafts](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vinijoncrafts/32/14306_2.png) [@vinijoncrafts](https://discourse.processing.org/u/vinijoncrafts)\
**Post date:** [December 26, 2022, 6:49pm UTC](https://discourse.processing.org/t/8-directional-movement-in-object/40317/1 "2022-12-26T18:49:13Z")

</div>

I need to make my player move in all of 8 directions using W A S D and my code makes some weird stuff when I press more than one key and I don’t know why

```auto
class Vini {

  float x = width/2;
  float y = height/2;
  float vel = 10;
  boolean [] keys = new boolean[128];

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

  void movement() {
    if (keyPressed) {

      keys[key]= true;

      if (keys['w']) {
        y -= vel;
      }
      if (keys['s']) {
        y += vel;
      }
      if (keys['a']) {
        x -= vel;
      }
      if (keys['d']) {
        x += vel;
      }
    } else if (keyPressed == false) {
      keys[key] = false;
    }
  }
}

Vini vini;

void setup() {
 size(600, 600);
 vini = new Vini();
}

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

```

if anyone could helpme I would be very grateful 👍

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [December 26, 2022, 7:35pm UTC](https://discourse.processing.org/t/8-directional-movement-in-object/40317/2 "2022-12-26T19:35:59Z")

</div>

> [@vinijoncrafts](#):
>
> my code makes some weird stuff when I press more than one key

Hello!  
I ran your code. Confirming the directional movement is not consistent or as expected on MacOS, processing 4.0.1.

🤓

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [December 26, 2022, 7:36pm UTC](https://discourse.processing.org/t/8-directional-movement-in-object/40317/3 "2022-12-26T19:36:08Z")

</div>

Hi @vinijoncrafts,

Move the key handling out of your class…

```auto
boolean[] keys = new boolean[255];

void keyPressed() {
  keys[keyCode] = true;
}

void keyReleased() {
  keys[keyCode] = false;
}

```

Your movement method then should look like…

> [@vinijoncrafts](#):
>
> ```auto
> void movement() {
> if (keys['w']) {
> y -= vel;
> }
> if (keys['s']) {
> y += vel;
> }
> if (keys['a']) {
> x -= vel;
> }
> if (keys['d']) {
> x += vel;
> }
> }
> 
> ```

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 26, 2022, 7:57pm UTC](https://discourse.processing.org/t/8-directional-movement-in-object/40317/4 "2022-12-26T19:57:03Z")

</div>

> **[r/processing - How would I make a keyPressed function support multiple keys?](https://www.reddit.com/r/processing/comments/bshqa8/comment/eor5epo/)**
>
> 2 votes and 13 comments so far on Reddit

---

<div class="post-metadata">

**Author:** ![vinijoncrafts](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vinijoncrafts/32/14306_2.png) [@vinijoncrafts](https://discourse.processing.org/u/vinijoncrafts)\
**Post date:** [December 27, 2022, 6:26pm UTC](https://discourse.processing.org/t/8-directional-movement-in-object/40317/5 "2022-12-27T18:26:51Z")

</div>

then I should put these keypressed/released at the end of the code? If yes, then thank you!

---

<div class="post-metadata">

**Author:** ![vinijoncrafts](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vinijoncrafts/32/14306_2.png) [@vinijoncrafts](https://discourse.processing.org/u/vinijoncrafts)\
**Post date:** [December 27, 2022, 7:05pm UTC](https://discourse.processing.org/t/8-directional-movement-in-object/40317/6 "2022-12-27T19:05:03Z")

</div>

yoooooo it worked thanks a lot!
