# Trying to get object to collide with player

**URL:** <https://discourse.processing.org/t/trying-to-get-object-to-collide-with-player/17142>\
**Category:** Coding Questions\
**Created:** [January 17, 2020, 4:32pm UTC](https://discourse.processing.org/t/trying-to-get-object-to-collide-with-player/17142 "2020-01-17T16:32:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Banana132](https://avatars.discourse-cdn.com/v4/letter/b/d9b06d/32.png) [@Banana132](https://discourse.processing.org/u/Banana132)\
**Post date:** [January 17, 2020, 4:32pm UTC](https://discourse.processing.org/t/trying-to-get-object-to-collide-with-player/17142/1 "2020-01-17T16:32:55Z")

</div>

Hi there, hoping someone is able to help with this issue I have, I am making a very basic game for an assignment and I am struggling to get an object to collide with the player to end the game. I have use the same collision detection in a previous assignment but it does not work for this game. The method im using probably is not the best but is there a better way to come by collision between the objects?

Below is from the main class

```auto
//game mode variables
int gameMode;
int BEGIN = 1;
int END = 0;

void draw()
{

  if (gameMode == BEGIN)
  {
    drawBackground();
    if (!player_1.hit());
    player_1.update();
    spaceRock1.update();
  }

  if (player_1.hit() == true)
  {
    gameMode = END;
    fill(255);
    textSize(32);
    text("Get REKT", width/2, height/2);
    text("Press SPACE to restart", width/2, height/2+50);
  }
}

```

Below is from the “player” class

```auto
int shipFront = y + 75;
  boolean hit()
  {
    color colorIdentify;
    for (int i=y; i < y+shipFront; i++)
    {
      colorIdentify = get(shipFront, i);
      if (colorIdentify == colour1)
      {

        return true;
      }
    }

    return false;
  }

```

and finally below is the colour im checking against if it is near the front of the player

color colour1 = color(211, 211, 211);

If all the code is needed for the whole game, i can upload it but the code above is just the collision that doesnt work for me ☹

Thanks in advance 🙂

---

<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 17, 2020, 5:00pm UTC](https://discourse.processing.org/t/trying-to-get-object-to-collide-with-player/17142/2 "2020-01-17T17:00:39Z")

</div>

> [@Banana132](#):
>
> shipFront

This variable is used as x in get:

colorIdentify = get(shipFront, i);

It is defined as … y… which puzzles me.

Also when the last line is inside the class the y you get is not the current one but probably 0.

**Also:**

You have a semicolon ; too much

> [@Banana132](#):
>
> if (!player\_1.hit());

With this ; the part you execute is non existent (the section between the bracket ) and ; only). Get rid of the ; to make the next line part of the if-clause. If that’s what you want, what I don’t know.

---

<div class="post-metadata">

**Author:** ![Banana132](https://avatars.discourse-cdn.com/v4/letter/b/d9b06d/32.png) [@Banana132](https://discourse.processing.org/u/Banana132)\
**Post date:** [January 17, 2020, 6:19pm UTC](https://discourse.processing.org/t/trying-to-get-object-to-collide-with-player/17142/3 "2020-01-17T18:19:47Z")

</div>

I got rid of the extra ‘;’ and i had also changed the (shipFront, i), to ( i, shipFront) and it works now, i completely forgot to swap them around so it works now, thank you so much ! 🙂
