if(keyPressed ) interrupted by pressing a 2nd key? How to fix this?

edited August 2015 in Questions about Code

Hello, just learning the basics right now. Making a little game based on the Car tutorial in which a car (rectangle) moves left-to right across the screen and obstacles appear in it's path. No collision detection or anything yet, but noticed a strange thing about using if(keyPressed){} to move up and down.

If I am holding a key, for example DOWN, and while holding it, press and release any other key, it interrupts the original keypress and the 'car' stops moving up or down.

If I press and hold UP while holding down, it will go up, but after releasing, it does not start to go down again. I understand that keyPressed only calls the first time a key is pressed, but how do I make sure that it will go back to a key that is being held?

Do I just need to correct my code, does this require additional code to work around, or is this just a quirk of Processing that I should learn to deal with?

Here's my (unfinished) code:

Car myCar;
Obstacle obstacle1;
boolean isCar = false;
float obstacleX;
float obstacleY;

void setup(){
   size(800,350);


}

void draw(){
   background(255);
  if(mousePressed){
    myCar = new Car(mouseX, mouseY);
    isCar = true;
    generateObstacle();
  }
  if(isCar){{
    myCar.move();
  }
  myCar.drive();
  myCar.display();
  obstacle1.display(obstacleX, obstacleY);
  }


}

void generateObstacle(){
    obstacle1 = new Obstacle();
    obstacleX = random(width/2, width);
    obstacleY = random(height);
}

class Car{
 float xpos;
 float ypos;
 float xspeed = 1;
 float yspeed = 2;
 color c = 0;

 Car(float tempX, float tempY){
   xpos=tempX;
   ypos=tempY;
 }

void display(){
  rectMode(CENTER);
  fill(c);
  rect(xpos,ypos,20,10);
}
void drive(){
  xpos = xpos + xspeed;
  if(xpos > width){
    xpos = 0;
    generateObstacle();
    increaseSpeed();
  }
}

void increaseSpeed(){
 xspeed += .5;
 yspeed += .5; 
}
void changeColor(){
  c = 120;
}

void move(){
  if(keyPressed){
  if(key == CODED){
    if(keyCode == UP){
      this.ypos -= yspeed;
    } 
     if (keyCode == DOWN){
      this.ypos += yspeed;
    }
  }
    if(ypos > height){
      ypos=0;
    }
    if(ypos < 0){
      ypos = height;
    }
  }
}

}

class Obstacle{
 float xpos;
 float ypos;
 color c = 120;

 void display(float x, float y){
   xpos= x;
   ypos= y;
  rectMode(CENTER);
  fill(c);
  rect(xpos,ypos,20,10);
}
}

Answers

  • Answer ✓

    It's great that you use keyPressed (the boolean), but you should start using keyPressed() (the function) (and keyReleased() too). Using them, you can set other booleans that can then be used to determine how your car should move.

    boolean[] keys = {
      false, false, false, false
    };
    
    void setup() {
      size(400, 400);
    }
    
    void draw() {
      background(0);
      if ( keys[0] ) text("UP", 20, 20);
      if ( keys[1] ) text("DOWN", 20, 40);
      if ( keys[2] ) text("LEFT", 20, 60);
      if ( keys[3] ) text("RIGHT", 20, 80);
    }
    
    void keyPressed(){
      if(keyCode == UP) keys[0] = true;
      if(keyCode == DOWN) keys[1] = true;
      if(keyCode == LEFT) keys[2] = true;
      if(keyCode == RIGHT) keys[3] = true;
    }
    
    void keyReleased(){
      if(keyCode == UP) keys[0] = false;
      if(keyCode == DOWN) keys[1] = false;
      if(keyCode == LEFT) keys[2] = false;
      if(keyCode == RIGHT) keys[3] = false;
    }
    
  • That worked perfectly! I had tried keyPressed() (the function) earlier, but with poor results. Using booleans like that makes perfect sense. Thank you!

Sign In or Register to comment.