Trying to get object to collide with player

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

//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

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 :frowning:

Thanks in advance :slight_smile:

1 Like

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

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.

4 Likes

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 ! :slight_smile:

2 Likes