I created a game for a course this last semester, but wanted to add a few things before being officially done with it on my own. One of those things is adding the ability for enemies to fire projectiles that can damage/kill the player character.
There were already player projectiles, so enemy projectiles started from a copy of the same code. However, I have run into problems with timing the enemy projectiles, and with the number of enemy projectiles.
I want the enemies to be able to fire 3 or so shots, and then pause for a short period, before being able to fire 3 more shots. This has been frustrating to try and figure out because of the second issue.
The enemies seem to share the projectiles and the timers with each other. For example, I give them 6 projectiles, and there is 3 enemies in a room. They will all fire but they won’t fire at the same time, it will be one or the other is firing.
I have tried making each enemy have an array of projectiles so they each have their own array, but I couldn’t figure out how to make it so each enemy has a separate array instead of them all sharing one.
there is a timer that determines how often the projectiles fire, and I will be re-adding a second timer to hopefully provide the pause I want between the sets of shots once I get the enemies firing on their own instead of sharing the projectiles.
within the enemy class constructor is
enemyprojectiles = new EnemyProjectile[6];
for (int n = 0; n < enemyprojectiles.length; n++) {
enemyprojectiles[n] = new EnemyProjectile(-400, -600);
enemyprojectiles[n].reset();
}
nextEnemyProjectile = 0;
and in the function that makes the enemies move/shoot is the projectiles themselves
if (vx == 0 && vy == 0 && y <= p.y+345 && y >= p.y-345) {
if (enemyFireTimer.complete()) {
if (nextEnemyProjectile <= enemyprojectiles.length) {
enemyprojectiles[nextEnemyProjectile].fire(x, y+(h-250), enemyfaceleft);
nextEnemyProjectile = (nextEnemyProjectile+1)%enemyprojectiles.length;
enemyFireTimer.start();
}
}//end firingTimer
}//end projectile shooting
//projectiles management
for (int l=0; l < enemyprojectiles.length; l++) {
enemyprojectiles[l].update();
if (playerprojectileIntersect(enemyprojectiles[l], p) && enemyprojectiles[l].inMotion) {
fill(193, 10, 10);
rect(0, 0, width, height);
enemyprojectiles[l].reset();
}//end rectangeIntersect
enemyprojectiles[l].display();
}
I am used to sharing the whole project in a zip file so I am not sure if this is enough information, please let me know if there’s anything else I need to provide let me know.