We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi all,
I'm very new to processing and am having some difficulty moving my objects the way I'd like to.
After pressing 'a' id like my triangle to permanently switch it's course from downwards to the direction entered, until I give it a new direction command. Also, i'd like the angle at which it turns to increase if I press 'a' twice. What am I doing wrong?
Thanks in advance for any help you can offer.
`int x = 200; int y =100; int a_count = 0;
void setup() {
size(500, 500);
}
void draw() {
background(255);
triangle(x-10, y-10, x+10, y-10, x, y+10); y++;
if (keyPressed) {
if (key == 'a') {
a_count++;
if (a_count == 1) {
triangle(x, y-10, x+10, y+2, x-8, y+8);
x-=2;
y+=2;
}
else if (a_count == 2){
triangle(x+10, y-10, x+10, y+10, x-10, y);
x-=4;
y+=1;
}
}
}
}
`
Answers
You might consider using keyPressed() instead of keyPressed. Here is an example that counts how many times the 'a' key has been pressed. If it is more than 10 times, something else happens:
Many thanks! I've switched it to keyPressed(), but it isn't 'continuing the new direction'. It kind of just bumps the shape to the left and goes back to it's downward direction. How do I make it maintain it's new course?
http://studio.ProcessingTogether.com/sp/pad/export/ro.9Nl$898UQxW3Q
https://forum.Processing.org/two/discussion/16868/change-direction-of-a-falling-triangle
Post your changes, we can't guess what the code looks like now.
And format it nicely - highlight it and press ctrl-o
Now that keyPressed() is dealing with counting your a key presses, you will want to constantly do different things in draw() based on how many you have seen so far. In short, you moved too much logic from draw() into keyPressed() - look at the count variable in my example again. See how it's value is used inside draw to see when the background should be red? You need to do something similar; when a_count is 1, go one way, when it is 3, go the other...
One of the links posted brought me to a better realized version of what I was trying to accomplish. Using their code, I've added code into keyPressed() to balance out the load, but I'm still finding myself trapped. It works fine the first run. One tap on the left key turns it slightly left and two taps turns it far left.
Now lets say I turn the plane right to even out the course, then I hit left again. It skips the slight left turn and goes directly to the sharp left turn. How can I make it reset the cycle of making a partial left and then a full left?