#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Change direction of a falling triangle

Howdy,

What I've been trying to do is to have a triangle fall to the bottom of the screen while being able to control it left and right. If the triangle is going down and the left key is pressed, it should go downLeft and if the left key is pressed again, it should point in the fully left position. Pressing the right key will reverse all of this and eventually point the triangle in the fully right position.

I've only been able to make it go left or right once (downLeft and downRight) but am unsure of how to make it go further left and further right.

Below is what I have already. I would be very grateful if anyone could point me in the right direction. Thank you

``````int x, y;

void setup() {
size(800,800);
x = width/2;
}

void draw () {
background(255);
if (keyCode == LEFT) {
downLeft();
} else if (keyCode == RIGHT) {
downRight();
} else {
down();
if (keyPressed) {
y = 0;
x = width/2;
}
}
}

void keyPressed() {
}

void down() {
triangle(x-20, y-20, x+20, y-20, x, y+20);
y+=5;
}

void downLeft() {
triangle(x, y-20, x+20, y+4, x-16, y+16);
x-=2;
y+=3;
}

void left() {
triangle(x+20, y-20, x+20, y+20, x-20, y);
x-=4;
y++;
}

void downRight() {
triangle(x, y-20, x-20, y+8, x+16, y+16);
x+=2;
y+=3;
}

void right() {
triangle(x-20, y-20, x-20, y+20, x+20, y);
x+=4;
y++;
}
``````
Tagged:

## Answers

• Thank you for replying.

From what I understand the code in that link actually rotates the object? Is there any way I could have it so that the arrow keys "cycles" through the different triangles in the functions I pasted above?

• Dunno whether it helps, but here's some online sketch w/ a triangle() "drivable" car: O:-)
http://studio.ProcessingTogether.com/sp/pad/export/ro.9Nl\$898UQxW3Q

• Howdy,

Thanks for your program I might try implementing that if I can't figure this out.

I took a look at this learningprocessing.com/examples/chp05/example-05-08-edgespath and tried modifying my code in draw() but am currently stuck:

``````int state;

void draw () {
background(255);
if (state == 0) {
down();
if (keyCode == LEFT) {
state = -1;
}
} else if (state == -1) {
downLeft();
if (keyCode == RIGHT) {
state = 0;
}
if (keyCode == LEFT) {
state = -2;
}
} else if (state == -2) {
left();
}
}
``````

Basically the triangle goes down, pressing the left key makes it go left() but it skips downLeft(). Changing it to a different key works but I'm only allowed to use the left and right keys. Does anyone know how to fix this? Thank you :)

• Nevermind I gots it I had to put some code in keyPressed()

• Hey bud,

I'm stuck on the same part. How did you get it all working? My triangle doesn't stick to the new course and just keeps going down after momentarily shifting direction?.

Cheers

Sign In or Register to comment.