In my game, I have an ArrayList of all the objects currently on the screen.
Every Object is extending my super class, GameObject, but not every object in the array are of the same class.
This is what I tried writing:
public int instance_number(Class<GameObject> obj) {
int count = 0;
ArrayList<GameObject> objects = game.getRoom().getObjects();
for (int i = 0; i < objects.size(); i++) {
GameObject other = objects.get(i);
if (other.getClass().equals(obj)) {
count++;
}
}
return count;
}
now, this would work, if all the objects in the array WERE GameObject’s, but they’re not, they’re only related to that class.
Using that code, in my player class, I tried using:
if (instance_number(Fireball.class) < 3) {
//stuff
}
because I want to get the number of Fireballs in my objects array. (Fireball extends GameObject).
Eclipse then told me I needed to make the instance_number method into this:
public int instance_number(Class<Fireball> class1) {
Is there anyway I can get my instance_number method working, without having to make a separate method for every class type?