# Player Health & Collision not working properly together

**URL:** https://discourse.processing.org/t/player-health-collision-not-working-properly-together/27244
**Category:** Coding Questions
**Tags:** homework
**Created:** [January 20, 2021, 6:14pm UTC](https://discourse.processing.org/t/player-health-collision-not-working-properly-together/27244 "2021-01-20T18:14:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![vvitch](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@vvitch](https://discourse.processing.org/u/vvitch)
#### Post date: [January 20, 2021, 6:14pm UTC](https://discourse.processing.org/t/player-health-collision-not-working-properly-together/27244/1 "2021-01-20T18:14:06Z")

</div>

For some reason, in the game that I am attempting to make for a class project whenever the player hits an asteroid at speed, the playerHealth variable goes down by multiple numbers instead of by one. The below are functions from my Asteroid class.

```auto
void collision() {
    if (asteroidX <= 0) {
      respawn();
    } 
    
    //if (!invincible) { //unimplemented invincibility frames
      for (int i = 0; i < asteroids.length; i++) {
        if (dist(asteroids[i].asteroidX, asteroids[i].asteroidY, player.playerX, player.playerY) < asteroids[i].asteroidSize + player.playerSize) {        
          respawn();
          playerHealth -= 1;
          println(playerHealth);
       // }
      }
    }
  }

  void respawn() {
    asteroidX = width*1.3; //spawn beyond screen
    asteroidY = random(height);
    asteroidSize = random(10, 25);
    asteroidSpeed= random(2, 5);
  }

```

Does anyone know why exactly this is happening? I have a vague feeling but I’m too new at this to really understand what exactly is to be fixed. I’m down for posting my full code if that’s necessary.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 20, 2021, 6:31pm UTC](https://discourse.processing.org/t/player-health-collision-not-working-properly-together/27244/2 "2021-01-20T18:31:59Z")

</div>

> [@vvitch](#):
>
> `respawn()`

respawn works on some variables but it doesn’t work on asteroids[i].asteroidX, asteroids[i].asteroidY

Why?

---

<div class="post-metadata">

### Author: ![vvitch](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@vvitch](https://discourse.processing.org/u/vvitch)
#### Post date: [January 20, 2021, 6:46pm UTC](https://discourse.processing.org/t/player-health-collision-not-working-properly-together/27244/3 "2021-01-20T18:46:30Z")

</div>

Thank you for pointing me in the right direction! I changed the contents of the for() loop in my collisions and it all works as intended now.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 20, 2021, 9:25pm UTC](https://discourse.processing.org/t/player-health-collision-not-working-properly-together/27244/4 "2021-01-20T21:25:41Z")

</div>

What I meant was:

This function:

> [@vvitch](#):
>
> ```auto
> void respawn() {
> asteroidX = width*1.3; //spawn beyond screen
> asteroidY = random(height);
> asteroidSize = random(10, 25);
> asteroidSpeed= random(2, 5);
> }
> 
> ```

This should look like this:

```auto
  void respawn( int i ) {
    asteroids[i].asteroidX = width*1.3; //spawn beyond screen
    asteroids[i].asteroidY = random(height);
    asteroids[i].asteroidSize = random(10, 25);
    asteroids[i].asteroidSpeed= random(2, 5);
  }

```

THEN the asteroid that collided will get a new position and **can’t count for the health again**

**Remark**

These variables from respawn() like asteroidX asteroidY etc. shouldn’t really exist…

you got all the information in the class and in the array asteroids

**Hey, and welcome to the forum!**

**Great to have you here!**

Chrisir
