Questions on how to redirect my object using the same key twice

edited June 2016 in Questions about Code

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

Tagged:

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:

    int count = 0;
    
    void setup(){
      size(220,220);
      textSize(32);
      textAlign(CENTER,CENTER);
      fill(255);
    }
    
    void draw(){
      background(0);
      if(count > 10) {
        background(255,0,0);
      }
      text(count,100,100);
    }
    
    void keyPressed(){
      if(key=='a'){
        count++;
      }
    }
    
  • 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?

  • Post your changes, we can't guess what the code looks like now.

    And format it nicely - highlight it and press ctrl-o

  • `int x = 200;
    int y =100; 
    int a_count = 0;
    
    
    void setup() {  
      size(500, 500);
    }
    
    void draw() { 
    background(200);
    
    triangle(x-10,y-10,x+10,y-10,x,y+10);
    y = y+=1;
    }
    
    
    void keyPressed()  {
        if (key == 'a') {
          a_count++;
    
             if (a_count == 1) {
    background(200);
              fill(0);
              triangle(x, y-10, x+10, y+2, x-8, y+8);
              x = x-=2;
              y = y+=2;
          }
            if(a_count == 2){
      triangle(x+10, y-10, x+10, y+10, x-10, y);
      x = x-=4;
      y = y+=1;
        }
       }
      }
    `
    
  • 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...

  • edited June 2016

    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?

    `    int x, y;
            int count = 0;
    
            void setup() {
              size(800,800);
              x = width/2;
            }
    
            void draw () {
              background(255);
              if (keyCode == LEFT && count == 1) {
                downLeft();
              }
                else if ( keyCode == LEFT && count <=2) {
                  left(); }
                  else if ( keyCode == LEFT && count >=2) {
                    count = 0;} 
               else if (keyCode == RIGHT) {
                downRight();
              } else {
                down();
                if (keyPressed) {
                  y = 0;
                  x = width/2;
                }
              }
            }
    
            void keyPressed() {
              if(keyCode == LEFT){
                count++;
                if(count >=2) {
                  count = 0;
                }
              }
            }
    
            void down() {
              triangle(x-20, y-20, x+20, y-20, x, y+20);
              y+=1;
            }
    
            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++;
            }`
    
Sign In or Register to comment.