# 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.

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

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!

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;
}
}
``````

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

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