cant seem to get my enemies shooting [arraylist]

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

Yes. I once created a game in which I put the projectile list in the mob class, but as soon as a mob got destroyed, all pending projectiles were deleted. It looked really weird.

Can you show the method that actually shoots the projectile?

public void update() {
		y += speedY;

		 System.out.println("projectileEnemy update");
		enemyProjectileRect.setRect(x, y, enemyProjectileRectX,
				enemyProjectileRectY);

		if (y < -30 || y > 800) {
			visible = false;
			// projectileRect = null;

		}

		// groter dan -30??
		if (y > -30 && y < 801) {
			checkCollision();
		}

	}

This is the update method in my ProjectileEnemy class which actually never gets reached ( System.out.println(“projectileEnemy update”); isnt printing…)

In my main game class i try to update my enemyProjectiles arraylist in the same place and in the same way as i update the projectiles shot by my hero Popolon (which works fine):

ArrayList projectiles = popolon.getProjectiles();
				for (int i = 0; i < projectiles.size(); i++) {
					Projectile p = (Projectile) projectiles.get(i);
				
					if (p.isVisible() == true) {
						p.update();
					} else {
						projectiles.remove(i);
					}
				}
				
				ArrayList enemyProjectiles = enemy.getProjectiles();
				//System.out.println("KMG enemyProjectiles.size()=" +enemyProjectiles.size());
				for (int k = 0; k < enemyProjectiles.size(); k++) {
					
					
					ProjectileEnemy pe = (ProjectileEnemy) enemyProjectiles.get(k);
							
					if (pe.isVisible() == true) {
						pe.update();
					} else {
						//enemyProjectiles.remove(k);
					}
				}

but I guess its not working since enemy.getProjectiles(); is 0

Have you checked if the projectile is visible or not? Because it won’t update otherwise.

yes i did and i also tried calling the update method outside of the if statement, but the update mehtod isnt reached.
I suppose because enemy.getProjectiles() keeps returning 0 hence there are no instances that can be called to update from my maingame class

This is how I would do it.

Have an array of projectiles stored in a class, such as a World class. Have your enemies be constructed, pass them this projectile array.

When they fire, simply add the projectile to that array. Since objects get passed as references this works well.

Update said array in the World class.

I would not recommend having your enemies hold their own array of projectiles, if the enemy gets deleted from the world then he is no longer updating his projectiles, which can end in weird results, such as all projectiles just stopping on the spot or getting swooped up by the GC.

You could also have the Enemy class hold a static array, then in your World class just update it using Enemy.projectilesEnemies.update(). This will save you from passing the array to each enemy, in-case your code is messy and your enemies are being created all over the place (this is bad in its own right).

uhhhh woops…
It was that simple?!

I simply moved my arraylist over to my main (knightmareGame-) class

static ArrayList<ProjectileEnemy> projectilesEnemies = new ArrayList<ProjectileEnemy>();

(like i thought i had done before but without result) and add a projectile in my checkEnemyShoots method in my enemy class whenever an enemy shoots:

KnightmareGame.projectilesEnemies.add(pe);

And now my enemies are shooting projectiles.

Thanx for you guys’ help anyway. Sometimes I just makes things clearer when someone explains how he/she would do it.