Problem with procesing in game

Hey guys!
I have a little problem.
I’m making a game for classes. It’s similar to Space Invaders.
So when i’m press <- (to left) and -> (to right), sometimes it takes to move.
Does anyone have a solution?
My code for move:

void movNave(){
  //Movimiento de la nave
  if(keyPressed){
    if(!bullet){
      if(keyCode == 38){
        if(!bullet){
          bulletIndepe=pos[0]+40;
          bullet = true;
          numdisparos++;
          println(numdisparos);
        }
      }
    }
    if (keyCode == 39 && (pos[0]<=1080)){   //derecha
      if(tipoNave==1){
        image(navev1D, pos[0], pos[1]);
      }else if(tipoNave==2){
        image(navev2D, pos[0], pos[1]);
      }else{
        image(navev3D, pos[0], pos[1]);
      }
      pos[0] = pos[0]+indmov;
    }else if(keyCode == 37 && (pos[0]>=500)){ //izquierda
      if(tipoNave==1){
        image(navev1I, pos[0], pos[1]);
      }else if(tipoNave==2){
        image(navev2I, pos[0], pos[1]);
      }else{
        image(navev3I, pos[0], pos[1]);
      }
      pos[0] = pos[0]-indmov;
    }else{
      printarNave();
    }
  }else{
    printarNave();
  }
}

Im not sure what this question is asking, could you clarify please

It is hard to get the exact problem since you did not provide all your code but you can try to do something like this:

boolean moveLeft;
boolean moveRight;

void setup() {
  ...
  moveLeft = false;
  moveRight = false;
  ...
}

void draw() {
  ...
  if (moveLeft) {
    ...
  }
  if (moveRight) {
    ...
  }
  ...
}

void keyPressed() {
  if (keyCode == 37) { // not sure about the keyCode
    moveLeft = true;
  }
  if (keyCode == 39) { // not sure about the keyCode
    moveLeft = true;
  }
}

void keyReleased() {
  if (keyCode == 37) { // not sure about the keyCode
    moveLeft = false;
  }
  if (keyCode == 39) { // not sure about the keyCode
    moveLeft = false;
  }
}
1 Like