# Player movement is snappy

**URL:** <https://discourse.processing.org/t/player-movement-is-snappy/40331>\
**Category:** Coding Questions\
**Created:** [December 27, 2022, 8:16pm UTC](https://discourse.processing.org/t/player-movement-is-snappy/40331 "2022-12-27T20:16:35Z")\
**Posts on this page:** 5\
**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 27, 2022, 8:16pm UTC](https://discourse.processing.org/t/player-movement-is-snappy/40331/1 "2022-12-27T20:16:36Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [December 27, 2022, 8:37pm UTC](https://discourse.processing.org/t/player-movement-is-snappy/40331/2 "2022-12-27T20:37:16Z")

</div>

Hi

## Programming Movement with Keyboard Controls in Processing

[![](https://img.youtube.com/vi/tWL8OZrOS_k/maxresdefault.jpg "Programming Movement with Keyboard Controls in Processing") ](https://www.youtube.com/watch?v=tWL8OZrOS_k)

> **[keyCode / Reference](https://processing.org/reference/keycode)**
>
> The variable keyCode is used to detect special keys such as the UP, DOWN, LEFT, RIGHT arrow keys and ALT, CONTROL, SHIFT. When checking for these keys, it can be useful to first chec…

> **[keyPressed() / Reference](https://processing.org/reference/keypressed_)**
>
> The keyPressed() function is called once every time a key is pressed. The key that was pressed is stored in the key variable. For non-ASCII keys, use the keyCode varia…

> **[key / Reference](https://processing.org/reference/key.html)**
>
> The system variable key always contains the value of the most recent key on the keyboard that was used (either pressed or released). For non-ASCII keys, use the keyCode varia…

> **[keyReleased() / Reference](https://processing.org/reference/keyreleased_)**
>
> The keyReleased() function is called once every time a key is released. The key that was released will be stored in the key variable. See key and keyCode for more informati…

---

<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 28, 2022, 9:28am UTC](https://discourse.processing.org/t/player-movement-is-snappy/40331/3 "2022-12-28T09:28:57Z")

</div>

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

> [@vinijoncrafts](#):
>
> ```auto
> void movement() {
> 
> if (keys['w']) {
> y += vely;
> vely = -10; // should be before using
> }
> if (keys['s']) {
> y += vely;
> vely = 10;// should be before using
> }
> if (keys['a']) {
> x += velx;
> velx = -10;// should be before using
> }
> if (keys['d']) {
> x += velx;
> velx = 10;// should be before using
> }
> 
> ```

---

<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 28, 2022, 1:59pm UTC](https://discourse.processing.org/t/player-movement-is-snappy/40331/4 "2022-12-28T13:59:03Z")

</div>

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

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

---

<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 28, 2022, 2:41pm UTC](https://discourse.processing.org/t/player-movement-is-snappy/40331/5 "2022-12-28T14:41:27Z")

</div>

Hi @vinijoncrafts,

> [@vinijoncrafts](#):
>
> I thought the order didn’t matter at all!

It is important… 🙂

# 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

[Data types - Programming fundamentals - OCR - GCSE Computer Science Revision - OCR - BBC Bitesize](https://www.bbc.co.uk/bitesize/guides/znh6pbk/revision/2#:~:text=Sequence%20is%20the%20first%20programming,to%20a%20program%20performing%20incorrectly).
