I cant seem to get my enemies shooting & cant figure out how I can get it working.
Currently I’m trying to have my enemies shoot whenever they hit a certain y value(=50).
I’ve put:
private ArrayList<ProjectileEnemy> projectilesEnemies = new ArrayList<ProjectileEnemy>();
in my Enemy class’ constructor to create an arraylist that keeps track of the enemy projectiles.
And i have a method that checks if the enemy needs to shoot which gets called in the Enemy class update method.
public void checkEnemyShoots() {
if (enemyShoots == true) {
ProjectileEnemy pe = new ProjectileEnemy(x, y);
projectilesEnemies.add(pe);
System.out.println("nr. of enemy projectiles(enemy)= " + projectilesEnemies.size());
enemyShoots = false;
}
}
I’m checking here to see how many enemies get stored in my arraylist and the return value of .size() never exceeds 1.
If i put the system.out… code in front of my projectilesEnemies.add(pe); code it keeps returning 0 so somewhere my arraylist gets reset…
But even with a size of 1 i havent been able to put an enemyprojectile on screen, because:
public ArrayList<ProjectileEnemy> getProjectiles()
{
return projectilesEnemies;
}
(which is in my Enemy class) just keeps returning 0.
I assume the problem here has something to do with that of every instance of my enemyclass the constructor creates a new(empty) arraylist “ProjectileEnemy” and thus erases the previous arraylist.
Although i dont understand why in between the time that the 1st & 2nd Enemy instances are created getProjectiles(); also keeps returning 0.
private ArrayList<ProjectileEnemy> projectilesEnemies = new ArrayList<ProjectileEnemy>();
I also tried to put this code for the creation of this arraylist in my main game class, but also can’t get this to work.
…
Am I on the right track here of thinking I have to create my ProjectileEnemy arraylist somewhere else then in my Enemy class?
And if yes: where and how?
And how come getProjectiles() keeps returning 0 even though when I check with
System.out.println("nr. of enemy projectiles(enemy)= " + projectilesEnemies.size());
projectilesEnemies.size() returns 1??