Seems to be performance issues, but only with one player

I am currently in the process in making a modified version of capture the flag and a quarter way though the process I ran into a weird issue. For some reason when moving player 2, the yellow player, around the board the entire game freezes at times. This is odd because for several minutes I tested player one by only moving that one and there was no freezing. There are no error messages nothing either.

Edit: I found an issue:holding down the arrow keys to move is fine, using the wasd keys however holding them down causes it to freeze they must be single pressed?

int[] player1;
int[] player2;
int[] player1chicken1;
int[] player1chicken2;
int[] player1chicken3;
int[] player2chicken1;
int[] player2chicken2;
int[] player2chicken3;
int p1chicken1width = 50;
int p1chicken1height = 50;
int p1chicken2width = 50;
int p1chicken2height = 50;
int p1chicken3width = 50;
int p1chicken3height = 50;
int p2chicken1width = 50;
int p2chicken1height = 50;
int p2chicken2width = 50;
int p2chicken2height = 50;
int p2chicken3width = 50;
int p2chicken3height = 50;
int p1width = 50;
int p1height = 50;
int p2width = 50;
int p2height = 50;
void setup(){
  size(1200, 600);
  player1= new int[] {250, 250};
  player2= new int[] {950, 250};
  player1chicken1= new int[] {40, 250};
  player1chicken2= new int[] {40, 50};
  player1chicken3= new int[] {40, 500};
  player2chicken1= new int[] {1110, 250};
  player2chicken2= new int[] {1110, 50};
  player2chicken3= new int[] {1110, 500};
}

void draw(){
  //draw players
  fill(0);
  rect(0,0,width/2,height);
  fill(255,0,0);
  rect(600,0,width/2,height);
  //move players
  fill(255);
  rect(player1[0], player1[1], p1width, p1height);
  if (keyPressed) {
    switch(keyCode) {
    case LEFT:
      player1[0] -=10;
      break;
    case RIGHT:
      player1[0] +=10;
      break;
    case UP:
      player1[1] -=10;
      break;
    case DOWN:
      player1[1] +=10;
      break;
    }
  }
  
  fill(255,255,0);
  rect(player2[0], player2[1], p2width, p2height);
  if (keyPressed) {
    if (key=='a'){
      player2[0] -=10;
      }
    if (key=='s'){
      player2[0] +=10;
      }
    if (key=='w'){
      player2[1] -=10;
      }
    if (key=='d'){
      player2[1] +=10;
      }
    }
    
   if(player1[0]+25>1150){
     player1[0]-=25;
   }
   if(player1[0]-25<0){
     player1[0]+=50;
   }
   
   if(player2[0]+25>1150){
     player2[0]-=25;
   }
   if(player2[0]-25<0){
     player2[0]+=50;
   }
   
   if(player1[1]+25>550){
     player1[1]-=25;
   }
   if(player1[1]-25<0){
     player1[1]+=50;
   }
   
   if(player2[1]+25>550){
     player2[1]-=25;
   }
   if(player2[1]-25<0){
     player2[1]+=50;
   }
   ////////////////////////////////chickens
   fill(0,0,255);
   rect(player1chicken1[0],player1chicken1[1],p1chicken1width,p1chicken1height);
   
   fill(0,0,255);
   rect(player1chicken2[0],player1chicken2[1],p1chicken2width, p1chicken2height);
   
   fill(0,0,255);
   rect(player1chicken3[0],player1chicken3[1],p1chicken3width,p1chicken3width);
   
   
   fill(0,0,255);
   rect(player2chicken1[0],player2chicken1[1],p2chicken1width,p2chicken1width);
   
   fill(0,0,255);
   rect(player2chicken2[0],player2chicken2[1],p2chicken2width,p2chicken2width);
   
   fill(0,0,255);
   rect(player2chicken3[0],player2chicken3[1],p2chicken3width,p2chicken3width);
   
   //move chickens
   if(player1[0] + p1width/2 > player2chicken1[0] - p2chicken1width/2
   && player1[0] - p1width/2 < player2chicken1[0] + p2chicken1width/2
   && player1[1] + p1width/2 > player2chicken1[1] - p2chicken1height/2
   && player1[1] - p1width/2 < player2chicken1[1] + p2chicken1height/2 ){
     player2chicken1[0]=player1[0];
     player2chicken1[1]=player1[1];
   }
   
   if(player1[0] + p1width/2 > player2chicken2[0] - p2chicken2width/2
   && player1[0] - p1width/2 < player2chicken2[0] + p2chicken2width/2
   && player1[1] + p1width/2 > player2chicken2[1] - p2chicken2height/2
   && player1[1] - p1width/2 < player2chicken2[1] + p2chicken2height/2){
     player2chicken2[0]=player1[0];
     player2chicken2[1]=player1[1];
   }
   
    if(player1[0] + p1width/2 > player2chicken3[0] - p2chicken3width/2
   && player1[0] - p1width/2 < player2chicken3[0] + p2chicken3width/2
   && player1[1] + p1width/2 > player2chicken3[1] - p2chicken3height/2
   && player1[1] - p1width/2 < player2chicken3[1] + p2chicken3height/2){
     player2chicken3[0]=player1[0];
     player2chicken3[1]=player1[1];
   }
   //
   //
   //
   if(player2[0] + p2width/2 > player1chicken1[0] - p1chicken1width/2
   && player2[0] - p2width/2 < player1chicken1[0] + p1chicken1width/2
   && player2[1] + p2width/2 > player1chicken1[1] - p1chicken1height/2
   && player2[1] - p2width/2 < player1chicken1[1] + p1chicken1height/2){
     player1chicken1[0]=player2[0];
     player1chicken1[1]=player2[1];
   }
   
   if(player2[0] + p2width/2 > player1chicken2[0] - p1chicken2width/2
   && player2[0] - p2width/2 < player1chicken2[0] + p1chicken2width/2
   && player2[1] + p2width/2 > player1chicken2[1] - p1chicken2height/2
   && player2[1] - p2width/2 < player1chicken2[1] + p1chicken2height/2){
     player1chicken2[0]=player2[0];
     player1chicken2[1]=player2[1];
   }
   
   if(player2[0] + p2width/2 > player1chicken3[0] - p1chicken3width/2
   && player2[0] - p2width/2 < player1chicken3[0] + p1chicken3width/2
   && player2[1] + p2width/2 > player1chicken3[1] - p1chicken3height/2
   && player2[1] - p2width/2 < player1chicken3[1] + p1chicken3height/2){
     player1chicken3[0]=player2[0];
     player1chicken3[1]=player2[1];
   }
  }
1 Like

The problem comes from the fact that keycode or key hold the last key that was changed either because you pressed it or because you released it.

So because the way you coded your movement, you can actually go left or right by holding only the top arrow key.

One way to solve it would be by using a global variable to store the direction:

boolean[] direction = new boolean[4]; // 0: left, 1: top, 2: right, 3: down

And then you can use the keyPressed and keyReleased function to update the values:

void keyPressed() {  
  switch(keyCode) {
    case LEFT:
      direction[0] = true;
      direction[1] = false;
      direction[2] = false;
      direction[3] = false;
      break;
    case RIGHT:
      direction[0] = false;
      direction[1] = false;
      direction[2] = true;
      direction[3] = false;
      break;
    case UP:
      direction[0] = false;
      direction[1] = true;
      direction[2] = false;
      direction[3] = false;
      break;
    case DOWN:
      direction[0] = false;
      direction[1] = false;
      direction[2] = false;
      direction[3] = true;
      break;
    }
}

void keyReleased() {  
  switch(keyCode) {
    case LEFT:
      direction[0] = false;
      break;
    case RIGHT:
      direction[2] = false;
      break;
    case UP:
      direction[1] = false;
      break;
    case DOWN:
      direction[3] = false;
      break;
    }
}

finally you can use this in your code to move the player:

if (direction[0]) {
  player1[0] -=10;
}
if (direction[1]) {
  player1[1] -=10;
}
if (direction[2]) {
  player1[0] +=10;
}
if (direction[3]) {
  player1[1] +=10;
}

It is done quite quickly and there are things to improve but you get rid of your problem.

Here’s the full code:

int[] player1;
int[] player2;
int[] player1chicken1;
int[] player1chicken2;
int[] player1chicken3;
int[] player2chicken1;
int[] player2chicken2;
int[] player2chicken3;
int p1chicken1width = 50;
int p1chicken1height = 50;
int p1chicken2width = 50;
int p1chicken2height = 50;
int p1chicken3width = 50;
int p1chicken3height = 50;
int p2chicken1width = 50;
int p2chicken1height = 50;
int p2chicken2width = 50;
int p2chicken2height = 50;
int p2chicken3width = 50;
int p2chicken3height = 50;
int p1width = 50;
int p1height = 50;
int p2width = 50;
int p2height = 50;

boolean[] direction = new boolean[4]; // 0: left, 1: top, 2: right, 3: down
void setup() {
  size(1200, 600);
  player1= new int[] {250, 250};
  player2= new int[] {950, 250};
  player1chicken1= new int[] {40, 250};
  player1chicken2= new int[] {40, 50};
  player1chicken3= new int[] {40, 500};
  player2chicken1= new int[] {1110, 250};
  player2chicken2= new int[] {1110, 50};
  player2chicken3= new int[] {1110, 500};
}


void draw() {
  //draw players
  fill(0);
  rect(0, 0, width/2, height);
  fill(255, 0, 0);
  rect(600, 0, width/2, height);
  //move players
  fill(255);
  rect(player1[0], player1[1], p1width, p1height);

  fill(255, 255, 0);
  rect(player2[0], player2[1], p2width, p2height);
  //if (keyPressed) {
  //  println(keyCode);
  //  switch(keyCode) {
  //  case LEFT:
  //    player1[0] -=10;
  //    break;
  //  case RIGHT:
  //    player1[0] +=10;
  //    break;
  //  case UP:
  //    player1[1] -=10;
  //    break;
  //  case DOWN:
  //    player1[1] +=10;
  //    break;
  //  }

  //  if (key=='a') {
  //    player2[0] -=10;
  //  }
  //  if (key=='s') {
  //    player2[0] +=10;
  //  }
  //  if (key=='w') {
  //    player2[1] -=10;
  //  }
  //  if (key=='d') {
  //    player2[1] +=10;
  //  }
  //}
  if (direction[0]) {
    player1[0] -=10;
  }
  if (direction[1]) {
    player1[1] -=10;
  }
  if (direction[2]) {
    player1[0] +=10;
  }
  if (direction[3]) {
    player1[1] +=10;
  }


  if (player1[0]+25>1150) {
    player1[0]-=25;
  }
  if (player1[0]-25<0) {
    player1[0]+=50;
  }

  if (player2[0]+25>1150) {
    player2[0]-=25;
  }
  if (player2[0]-25<0) {
    player2[0]+=50;
  }

  if (player1[1]+25>550) {
    player1[1]-=25;
  }
  if (player1[1]-25<0) {
    player1[1]+=50;
  }

  if (player2[1]+25>550) {
    player2[1]-=25;
  }
  if (player2[1]-25<0) {
    player2[1]+=50;
  }
  ////////////////////////////////chickens
  fill(0, 0, 255);
  rect(player1chicken1[0], player1chicken1[1], p1chicken1width, p1chicken1height);

  fill(0, 0, 255);
  rect(player1chicken2[0], player1chicken2[1], p1chicken2width, p1chicken2height);

  fill(0, 0, 255);
  rect(player1chicken3[0], player1chicken3[1], p1chicken3width, p1chicken3width);


  fill(0, 0, 255);
  rect(player2chicken1[0], player2chicken1[1], p2chicken1width, p2chicken1width);

  fill(0, 0, 255);
  rect(player2chicken2[0], player2chicken2[1], p2chicken2width, p2chicken2width);

  fill(0, 0, 255);
  rect(player2chicken3[0], player2chicken3[1], p2chicken3width, p2chicken3width);

  //move chickens
  if (player1[0] + p1width/2 > player2chicken1[0] - p2chicken1width/2
    && player1[0] - p1width/2 < player2chicken1[0] + p2chicken1width/2
    && player1[1] + p1width/2 > player2chicken1[1] - p2chicken1height/2
    && player1[1] - p1width/2 < player2chicken1[1] + p2chicken1height/2 ) {
    player2chicken1[0]=player1[0];
    player2chicken1[1]=player1[1];
  }

  if (player1[0] + p1width/2 > player2chicken2[0] - p2chicken2width/2
    && player1[0] - p1width/2 < player2chicken2[0] + p2chicken2width/2
    && player1[1] + p1width/2 > player2chicken2[1] - p2chicken2height/2
    && player1[1] - p1width/2 < player2chicken2[1] + p2chicken2height/2) {
    player2chicken2[0]=player1[0];
    player2chicken2[1]=player1[1];
  }

  if (player1[0] + p1width/2 > player2chicken3[0] - p2chicken3width/2
    && player1[0] - p1width/2 < player2chicken3[0] + p2chicken3width/2
    && player1[1] + p1width/2 > player2chicken3[1] - p2chicken3height/2
    && player1[1] - p1width/2 < player2chicken3[1] + p2chicken3height/2) {
    player2chicken3[0]=player1[0];
    player2chicken3[1]=player1[1];
  }
  //
  //
  //
  if (player2[0] + p2width/2 > player1chicken1[0] - p1chicken1width/2
    && player2[0] - p2width/2 < player1chicken1[0] + p1chicken1width/2
    && player2[1] + p2width/2 > player1chicken1[1] - p1chicken1height/2
    && player2[1] - p2width/2 < player1chicken1[1] + p1chicken1height/2) {
    player1chicken1[0]=player2[0];
    player1chicken1[1]=player2[1];
  }

  if (player2[0] + p2width/2 > player1chicken2[0] - p1chicken2width/2
    && player2[0] - p2width/2 < player1chicken2[0] + p1chicken2width/2
    && player2[1] + p2width/2 > player1chicken2[1] - p1chicken2height/2
    && player2[1] - p2width/2 < player1chicken2[1] + p1chicken2height/2) {
    player1chicken2[0]=player2[0];
    player1chicken2[1]=player2[1];
  }

  if (player2[0] + p2width/2 > player1chicken3[0] - p1chicken3width/2
    && player2[0] - p2width/2 < player1chicken3[0] + p1chicken3width/2
    && player2[1] + p2width/2 > player1chicken3[1] - p1chicken3height/2
    && player2[1] - p2width/2 < player1chicken3[1] + p1chicken3height/2) {
    player1chicken3[0]=player2[0];
    player1chicken3[1]=player2[1];
  }
}


void keyPressed() {  
  switch(keyCode) {
    case LEFT:
      direction[0] = true;
      direction[1] = false;
      direction[2] = false;
      direction[3] = false;
      break;
    case RIGHT:
      direction[0] = false;
      direction[1] = false;
      direction[2] = true;
      direction[3] = false;
      break;
    case UP:
      direction[0] = false;
      direction[1] = true;
      direction[2] = false;
      direction[3] = false;
      break;
    case DOWN:
      direction[0] = false;
      direction[1] = false;
      direction[2] = false;
      direction[3] = true;
      break;
    }
}

void keyReleased() {  
  switch(keyCode) {
    case LEFT:
      direction[0] = false;
      break;
    case RIGHT:
      direction[2] = false;
      break;
    case UP:
      direction[1] = false;
      break;
    case DOWN:
      direction[3] = false;
      break;
    }
}
2 Likes