Having trouble with ArrayList cant find an answer

I am getting "The function hits() does not expect parameters


for (int i=bullet.size()-1;i>0;i--){
    b.display();
    b.update(); 
    for (int j=asteroid.size()-1;j>0;j--){
      if(bullet.get(i).hits(asteroid.get(i)))){ 

      }//end bullet.hits(asteroid) loop
    }//end for loop
}

The function in my bullet class;



void hits(){
      float d = dist(pos.x,pos.y,a.pos.x,a.pos.y);
      if( d<a.r){
        print("hit");
      }

and

does not fit!
you should pass the asteroids parameter.

void hits(){

must be

void hits(Bullet a){

Within the brackets you define the parameters

so if (asteroids.get(i)) is an ArrayList of PVectors and this is written globally, I have written the hits() in the bullet class and checking the distance between the bullet.x and bullet.y to the asteroid.x and asteroid.y.

If I add void hits(PVector pos){ which would be the bullet pvector in the class, i then get hits() expecting paramenters hits(PVector) but its an array of PVectors

We are talking about this line

Depending on what asteroids contains,
In void hits say PVector or Bullet as parameter

Parameters must match

To summarize: here is a function:

void hi(){
  println("hi");
}

Based on the empty () in its fist line, it does not expect parameters. You can call it like this:
hi();
…but not like this:
hi("hello");

Here is another function:

void word(String wrd){
  println(wrd);
}

It does expect parameters – the parameter wrd that appears in the () in the first line. You can call it like this:
word("hello");
…but not like this:
word();

1 Like