0

I'm creating a shooter game and im unsure on how to make it detect the fact that the bullet has hit the enemy. This is the boolean that was given

 boolean isShot(Bullet bullet) //is shot sequence
  {
    if (bullet!=null)
    {
      if (abs(this.x - bullet.x) < 20  &&
        abs(this.y - bullet.y) < 20)
        return true;
    }
    return false;
  }

and this is the part where i try make it detect the collision and make the enemy disappear but it keeps giving me errors no matter what i try.

 import java.util.ArrayList;

int score;
Player p1;
Enemy[] e= new Enemy[4];
ArrayList<Bullet> bullet = new ArrayList<Bullet>();

void setup()
{
  size (1000, 1000);
  p1= new Player(500,5, 40);
  e[1]= new Enemy(100,1000,3);
   e[2]= new Enemy(500,800,3);
    e[3]= new Enemy(700,700,3);
 //   b1= new Bullet(500,500,-5);
}

void draw()
{
  background(255);
  p1.render();
  e[1].render();
  e[1].move();
  e[2].render();
  e[2].move();
  e[3].render();
  e[3].move();
  text("Score:" + score,50,50);
  
  for (Bullet b: bullet)
  {
    b.render();
    b.move();
  }

if (e[1].isShot(Bullet))
{
  e[1]=null;
}

Its at the bottom of this peice of code. When i try put bullet in lowercase it says "the function isShot() expects paramaters like "isShot(Bullet)" but when i capitalise the B in bullet it tells me that bullet isnt a variable.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
a.m123
  • 1
  • 2
  • 3
    Please don't make more work for others by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the [CC BY-SA license](https://creativecommons.org/licenses/by-sa/4.0), for SE to distribute the content (regardless of your future choices). By SE policy, the non-vandalized version is distributed. Thus, any such destructive edits will be reverted. Please see [How does deleting work?](/help/what-to-do-instead-of-deleting-question) for more information on how deleting content works on this site. – Ryan M Jan 13 '22 at 03:01

2 Answers2

1

"bullet" with lowercase b is an array of Bullets, but isShot() expects a single Bullet object. And "Bullet" with capital B is not a variable, but a class (I suppose).

So, you either need to create another object of Bullet or use one of the Bullet object in your "bullet" ArrayList.

1

You are almost there. I suggest you use more meaningful names for your parameters. First of all, you have to populate the ArrayList bullet in your setup() or elsewhere you want in your code.

After this, if you want to check that condition using isShot function you should pass to it an instance of an Object, not the class Object itself.

This can be easily achieved by including the function in your for loop.

While it is always good to practice, I suggest you to first understand the basics of the language and then move to more complex examples (custom classes, arrays iterations and so on).

void setup()
{
  size (1000, 1000);
  p1= new Player(500,5, 40);
  e[1]= new Enemy(100,1000,3);
   e[2]= new Enemy(500,800,3);
    e[3]= new Enemy(700,700,3);
    bullet.add(new Bullet(500,500,-5)); // e.g. to populate array
}

void draw()
{
  background(255);
  p1.render();
  e[1].render();
  e[1].move();
  e[2].render();
  e[2].move();
  e[3].render();
  e[3].move();
  text("Score:" + score,50,50);
  
  for (Bullet b: bullet)
  {
    b.render();
    b.move();

    if (e[1].isShot(b)) // now you can use b which is an instance of Bullet
    {
     e[1]=null;
    }
  }
Thecave3
  • 762
  • 12
  • 27