Processing Freezes when it runs through a specific if statement

Hey there, I’m new to Processing and have been trying to do an assignment for it about swatting flies, I have the fly swatter and a fly appearing on the screen and managed to get the fly swatter to follow my mouse position but when I do the mouse click over the fly the program will just freeze and I can’t do anything but close it. I’m not really sure whats going wrong here can I get some help?

I’ve narrowed it down to the collisionDetect function doing something weird with the if statement inside of it but not really sure why it would be causing it

Here is the code I currently have, it is part skeleton code that was provided by my school:

PImage fly,flybye,swatter,swatted;
float[] fX,fY;  // fly locations array
float[] swat;  // fly swatted binary boolean array, 1 = swatted, 0 = not swatted
int score=0;  // increments when swatted.


void setup(){
  size(800,400);
  fX=new float[0];
  fY=new float[0];
  swat=new float[0];
  // load images
  fly = loadImage("fly.png");
  fly.resize(50,50);
  flybye = loadImage("flybye.png");
  flybye.resize(50,50);
  swatter = loadImage("swatter.png");
  swatted = loadImage("swatted.png");
  
  fX =append(fX, random(50,750)); //first fly - random location
  fY =append(fY, random(25,375));
  swat =append(swat,0); // used as a boolean and matches to each individual fly, 0 = fly not swatted, 1 = swatted.
}

void populate(){ // draw the flies in memory to the screen.
  for(int i=0;i<fX.length;i++){
    if(swat[i]==1){ // if swatted
      // resize the fly image and place based on fx/fy array values
      image(flybye, fX[0], fY[0]);
    } else { // not swatted
      image(fly,fX[0],fY[0]);
    }
  }
}

void collisionDetect(){ //collision detection - detect collision between swatter and fly
  for(int i=0; i<swat.length;i++){ // bounding box detection
    if(mouseX-37 > fX[0]-40 && mouseX-37 < fX[0]+40 && mouseY-30 > fY[0]-40 && mouseY-30 < fY[0]+40){ // condition should look at location of mouse and individual coordinates in fX and fY
      swat[i] = 1; // swatted
      
      fX =append(fX, random(50,750)); //new fly placed in random location when old fly dies.
      fY =append(fY, random(50,350));
      swat =append(swat,0); // new fly not swatted
      score++; //increment score
    }
  }
}

void draw(){ 
  background(255);
  
  
  //noCursor(); //added to hide cursor for a more fluid look
  populate(); // draw flys to screen.
  fill(0);
  // set a text size and location for the score.
  if(mousePressed){ // image swap
    collisionDetect();
    image(swatted, mouseX-37, mouseY-30);   //draw swatter image to around mouse locaiton - might want to play with this to get it to look right.
    println("Error");
  }else{
    image(swatter, mouseX-37, mouseY-30); // if not pressed then alternative image.
  }
  
}

first try might be to disable lines what might cause the trouble:

//  collisionDetect();

if the program now runs at mouse click dig deeper:
enable again,

  collisionDetect();

disable

//  swat =append(swat,0); // new fly not swatted

and test again…
might be you find the critical line??


if you need to disable bigger sections of code can use also

/*
code not to be run
*/

I ended up doing this just after I posted actually and found that when I commented out the following lines:

for(int i=0; i<swat.length;i++){ // bounding box detection

swat[0] = 1; // swatted

swat =append(swat,0); // new fly not swatted

It would stop freezing but I get an ArrayIndexOutOfBoundsException: 1

If I commented out anything to do with the swat array it would work somewhat okay without error, though obviously not what I want to happen to meet what the assignment asks (leave a splatted bug behind and create a new one to be able to splat). The only issue is the swat stuff was part of the skeleton code provided so im not entirely sure whats wrong with it.

you might create a never ending story
by i < swat.length and swat append

Everytime you append to swat you increase the array length which is used in the for statement to see whether to terminate the loop. So what happens is you get an infinite loop as the goal posts (swat.length) keeps moving and you never reach them.

It is very bad practice to modify a collection while you are iterating over it.

That was part of the skeleton code that was provided by my school so I am not sure why it is there and not really sure how to fix it

if the teacher give you a buggy code it might THE exercise is: to find that bug??

The exercise is to create a fly swatter game with provided skeleton code, I don’t believe they would intentionally add buggy code when everything else worked fine.

you are aware that without your 4 image files we are not able to run that code,
so we can just give some tips.

so despite that a situation like that should not happen
also there is a dirty way out of it, as a loop can be broken.

as after

      score++; //increment score

the job is done ( as the mouse should not be over 2 fly at same time ?? )
and the for loop could end here,
you can stop the for loop with modifying adding

i = swat.length;

and it will stop.

One way would to get the array length before entering the for loop e.g.

void collisionDetect(){ //collision detection - detect collision between swatter and fly
  int n = swat.length; // now use this to control for loop
  for(int i=0; i<n; i++){ // bounding box detection
    if(mouseX-37 > fX[0]-40 && mouseX-37 < fX[0]+40 && mouseY-30 > fY[0]-40 && mouseY-30 < fY[0]+40){ // condition should look at location of mouse and individual coordinates in fX and fY
      swat[i] = 1; // swatted
      
      fX =append(fX, random(50,750)); //new fly placed in random location when old fly dies.
      fY =append(fY, random(50,350));
      swat =append(swat,0); // new fly not swatted
      score++; //increment score
    }
  }
}
2 Likes