Delay on first collision, but not subsequent ones

Hi all, I’m wondering if anyone can let me know why I get a considerable delay (a few seconds) when one object collides with another for the first time, but then after that there’s no delay.

Here’s my code:

PImage[] playerFrames = new PImage[9];
PImage[] zombieFrames = new PImage[6];

Player playerObject_Kate;
Zombie[] objectArray_Zombie = new Zombie[5];

void setup() {
  size(400, 400);
  imageMode(CENTER);

  for (int i=0; i < playerFrames.length; i++) {
    playerFrames[i] = loadImage("Fplayer-" + i + ".png");
  }
  for (int i=0; i < zombieFrames.length; i++) {
    zombieFrames[i] = loadImage("zombie-" + i + ".png");
  }

  playerObject_Kate = new Player();

  for (int i=0; i < objectArray_Zombie.length; i++) {
    objectArray_Zombie[i] = new Zombie();
  }
}

void draw() {
  background(0);  
  playerObject_Kate.move();
  playerObject_Kate.display();

  for (int i = 0; i < objectArray_Zombie.length; i++) {
    objectArray_Zombie[i].move(); 
    objectArray_Zombie[i].display();
  
    if (collision(objectArray_Zombie[i], playerObject_Kate) == true) {
      fill(255); 
      text("OUCH!", playerObject_Kate.x + 25, playerObject_Kate.y - 30);
    }
  }
}

//--------- FUNCTION: Collision test ----------//
boolean collision(Zombie z, Player p) {

  // collision test
  float d = dist(p.x, p.y, z.x, z.y); 
  if ( d < p.radius + z.radius) {    
    return true;
  } else {
    return false;
  }
}


// --------- PLAYER CLASS ---------//
class Player {
  float x;
  float y;  
  int frame = 0;
  int flip = 1; 
  float radius = 30;

  Player() {
  }

  void display() {
    pushMatrix();
    translate(x, y);
    scale(flip, 1); 
    image(playerFrames[frame], 0, 0);
    popMatrix();  

    if (frameCount % 4 == 0) frame++; // animate
    if (frame >= playerFrames.length) frame = 0; // loop
  }

  void move() {
    x = mouseX;
    y = mouseY; 
    if (mouseX > pmouseX) flip = 1;
    if (mouseX < pmouseX) flip = -1;
  }
}


// --------- ZOMBIE CLASS ---------//
class Zombie {
  int frame = 0;
  float speedX = random(-2, 2);
  float speedY = random(-2, 2);
  float x = random(width);
  float y = random(height);
  float radius = 30;

  Zombie() {
    while ( collision (this, playerObject_Kate) == true ) {
      x = random(width);
      y = random(height);
    }
  }

  void display() {
    pushMatrix();
    translate(x, y);
    if (speedX < 0) scale(-1, 1);
    image(zombieFrames[frame], 0, 0);   
    popMatrix();
  }

  void move() {
    x += speedX;
    y += speedY;
    if ( x < 0 || x > width ) speedX *= -1;
    if ( y < 0 || y > height ) speedY *= -1;
    if (frameCount % 4 == 0) frame++; // animate
    if (frame >= zombieFrames.length) frame = 0; // loop
  }
}

I’m not sure if we can upload data files to the forum. If we can let me know if that’s helpful for solving this issue.

Thanks!

1 Like

Hello,

I am not seeing a delay.

image
Fplayer-0
zombie-0

Ouch!

:)

I definitely am. Does anyone else know what the cause might be?

When text() is called for the 1st time, Processing has to select its default font if 1 hasn’t already. :snail:

As a workaround, you can call any text-related function within setup(), so the default font becomes ready. :bulb:

1 Like

Hello,

Did the suggestion offered by @GoToLoop correct this issue for you?
I am very interested…

Yes, that totally does the trick! I set the size in the setup:

textSize(10);

And the delay went away.

Thanks so much for your help GoToLoop!