How to move an Object at x and y simultaneously?

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. :slight_smile: Thanks

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();
}
1 Like

Try replacing your else if statements to simple if statements.

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! :slightly_smiling_face:

1 Like

Indeed it is. It worked for me! Thanks

Here is what i did.

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;
  }
}
1 Like

Excellent work! You’re a fast learner my friend! :grin:

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:

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

instead say:

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

if(wKey == true) {

instead say

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. :blush:

1 Like