# How to move an Object at x and y simultaneously?

**URL:** https://discourse.processing.org/t/how-to-move-an-object-at-x-and-y-simultaneously/25801
**Category:** Coding Questions
**Created:** [November 29, 2020, 1:46am UTC](https://discourse.processing.org/t/how-to-move-an-object-at-x-and-y-simultaneously/25801 "2020-11-29T01:46:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![corken](https://avatars.discourse-cdn.com/v4/letter/c/97f17d/32.png) [@corken](https://discourse.processing.org/u/corken)
#### Post date: [November 29, 2020, 1:46am UTC](https://discourse.processing.org/t/how-to-move-an-object-at-x-and-y-simultaneously/25801/1 "2020-11-29T01:46:29Z")

</div>

Hello

I dont quite understand how to move an object on the x and on the y at the same time.  
It would be nice if someone could give me an example.

Also here is the code im working on right now. If you want to correct it there.  
Always open for advice. 🙂 Thanks

```auto
int xpos = 50;
int ypos = 50;
int rectsize = 50;
int speed = 5;
boolean x = true;

void setup(){
  size(400,400);
  refresh();
}
void draw(){
  if(keyPressed == true){
    if(key ==UP || key == 'w'){
      if(ypos > 0){
        ypos-=speed;
      }
    }
    else if(key == DOWN || key == 's'){
      if(ypos < height-rectsize-1){
        ypos+=speed;
      }
    }
    else if(key == RIGHT || key == 'd'){
      if(xpos < width-rectsize-1){
        xpos+=speed;
      }
    }
    else if(key == LEFT || key == 97){
      if(xpos > 0){
        xpos-=speed;
    }
  }
  refresh();
}
  
}
void refresh(){
  background(0,0,0);
  drawrect(xpos,ypos);
}
void drawrect(int x, int y){
  fill(random(255),random(255),random(255));
  rect(x,y,50,50);
}
void keyReleased(){
  if(keyPressed == false){
    noLoop();
  }
}
void keyPressed(){
  loop();
}

```

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [November 29, 2020, 2:50am UTC](https://discourse.processing.org/t/how-to-move-an-object-at-x-and-y-simultaneously/25801/2 "2020-11-29T02:50:27Z")

</div>

Try replacing your else if statements to simple if statements.

---

<div class="post-metadata">

### Author: ![Math\_Machine](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@Math\_Machine](https://discourse.processing.org/u/Math_Machine)
#### Post date: [November 29, 2020, 4:09am UTC](https://discourse.processing.org/t/how-to-move-an-object-at-x-and-y-simultaneously/25801/3 "2020-11-29T04:09:20Z")

</div>

Within the draw function, “key” will only have one value, even if you’re pressing multiple keys simultaneously. Meanwhile, the keyPressed() function is run once every time a key is pressed down, and the keyReleased() function is run once every time a key is released. Every time keyPressed() is run, the variable “key” will become whatever key was just pressed, and every time keyReleased() is run, the variable “key” will become whatever key was just released.

My advice is to create a set of booleans, each of which are true if and only if a particular key is currently being held down. If your controls were limited to just WASD, we’d have 4 booleans: one for w, one for a, one for s, and one for d. Since we’re also using the arrow keys, we need to have 4 more booleans, one for up, one for down, one for left, and one for right. That’s 8 booleans total.

Using the keyPressed() and keyReleased() functions, it should hopefully be fairly straightforward how to do this. If you still need help, though, I have several code fragments which I’ve used over the years for similar issues. I do recommend, however, you try to figure this one out for yourself. Part of the fun of coding is figuring things out for yourself! 🙂

---

<div class="post-metadata">

### Author: ![corken](https://avatars.discourse-cdn.com/v4/letter/c/97f17d/32.png) [@corken](https://discourse.processing.org/u/corken)
#### Post date: [November 29, 2020, 11:49am UTC](https://discourse.processing.org/t/how-to-move-an-object-at-x-and-y-simultaneously/25801/4 "2020-11-29T11:49:42Z")

</div>

Indeed it is. It worked for me! Thanks

Here is what i did.

```auto
int xpos = 50;
int ypos = 50;
int rectsize = 50;
int speed = 5;
boolean wKey, aKey, sKey, dKey;

void setup(){
  size(400,400);
  refresh();
}
void draw(){
  refresh();
  if(wKey == true){
    if(ypos > 0){
      ypos-=speed;
    }
  }
  if(sKey == true){
    if(ypos < height-rectsize-1){
      ypos+=speed;
    }
  }
  if(dKey == true){
    if(xpos < width-rectsize-1){
      xpos+=speed;
    }
  }
  if(aKey == true){
    if(xpos > 0){
      xpos-=speed;
  }
}
  
}
void refresh(){
  background(0,0,0);
  drawrect(xpos,ypos);
}
void drawrect(int x, int y){
  fill(random(255),random(255),random(255));
  rect(x,y,50,50);
}
void keyReleased(){
  if(key == 'w'){
    wKey = false;
  }
  if(key == 'a'){
    aKey = false;
  }
  if(key == 's'){
    sKey = false;
  }
  if(key == 'd'){
    dKey = false;
  }
}
void keyPressed(){
  if(key == 'w'){
    wKey = true;
  }
  if(key == 'a'){
    aKey = true;
  }
  if(key == 's'){
    sKey = true;
  }
  if(key == 'd'){
    dKey = true;
  }
}

```

---

<div class="post-metadata">

### Author: ![Math\_Machine](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@Math\_Machine](https://discourse.processing.org/u/Math_Machine)
#### Post date: [November 30, 2020, 1:02am UTC](https://discourse.processing.org/t/how-to-move-an-object-at-x-and-y-simultaneously/25801/5 "2020-11-30T01:02:03Z")

</div>

Excellent work! You’re a fast learner my friend! 😁

A few quick notes, though: firstly, unless of course you planned on having more code within the “if(wKey==true)” statement, I recommend fusing your nested if statements into a single if statement using logical AND (&&). That is, rather, than saying:

```auto
if(wKey == true) {
  if(ypos > 0) {
    ypos-=speed;
  }
}

```

instead say:

```auto
if(wKey == true && ypos > 0) {
  ypos-=speed;
}

```

But again, this only applies if you didn’t plan on putting anything else in the outer if statement.

Secondly, instead of saying

```auto
if(wKey == true) {

```

instead say

```auto
if(wKey) {

```

Since, I mean…that’s kinda just the definition of a boolean. Trust me, I used to do that all the time, too. 😊
