# Delay on first collision, but not subsequent ones

**URL:** <https://discourse.processing.org/t/delay-on-first-collision-but-not-subsequent-ones/18989>\
**Category:** Coding Questions\
**Created:** [March 24, 2020, 6:28pm UTC](https://discourse.processing.org/t/delay-on-first-collision-but-not-subsequent-ones/18989 "2020-03-24T18:28:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SheCurvesMobius](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shecurvesmobius/32/7054_2.png) [@SheCurvesMobius](https://discourse.processing.org/u/SheCurvesMobius)\
**Post date:** [March 24, 2020, 6:28pm UTC](https://discourse.processing.org/t/delay-on-first-collision-but-not-subsequent-ones/18989/1 "2020-03-24T18:28:36Z")

</div>

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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [March 24, 2020, 6:58pm UTC](https://discourse.processing.org/t/delay-on-first-collision-but-not-subsequent-ones/18989/2 "2020-03-24T18:58:28Z")

</div>

Hello,

I am not seeing a delay.

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4999e0608b4d36cf863b04a7c59217f7edb11341.png)  
 ![Fplayer-0](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8d392aef15c7e4a5ec275bec9523bd56932e09ac.png)  
 ![zombie-0](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/79b3ebe040dbb26763a7f265a03409cad049b0a9.png)

Ouch!

`:)`

---

<div class="post-metadata">

**Author:** ![SheCurvesMobius](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shecurvesmobius/32/7054_2.png) [@SheCurvesMobius](https://discourse.processing.org/u/SheCurvesMobius)\
**Post date:** [March 24, 2020, 7:41pm UTC](https://discourse.processing.org/t/delay-on-first-collision-but-not-subsequent-ones/18989/3 "2020-03-24T19:41:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 24, 2020, 9:47pm UTC](https://discourse.processing.org/t/delay-on-first-collision-but-not-subsequent-ones/18989/4 "2020-03-24T21:47:51Z")

</div>

> [@SheCurvesMobius](#):
>
> … why I get a considerable delay (a few seconds) when one object collides with another for the first time,

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

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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [March 25, 2020, 1:22pm UTC](https://discourse.processing.org/t/delay-on-first-collision-but-not-subsequent-ones/18989/5 "2020-03-25T13:22:34Z")

</div>

Hello,

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

---

<div class="post-metadata">

**Author:** ![SheCurvesMobius](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shecurvesmobius/32/7054_2.png) [@SheCurvesMobius](https://discourse.processing.org/u/SheCurvesMobius)\
**Post date:** [March 28, 2020, 3:12pm UTC](https://discourse.processing.org/t/delay-on-first-collision-but-not-subsequent-ones/18989/6 "2020-03-28T15:12:42Z")

</div>

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

```auto
textSize(10);

```

And the delay went away.

Thanks so much for your help GoToLoop!
