Problem with spawning to many objects
in
Programming Questions
•
5 months ago
Hello, I am making a video game for a class project and ran across a big problem! I will past the code below. Basically, I am adding bullets to an array to move them once they are fired. This is the section that fires the enemy bullets. I want the fire certain types every 1-3 seconds. I am using frameCount % == 60, 120, 180... etc to control this. The problem however is that once something is fired it adds 100's of bullets to the arrays(I saw this through println statements). I am using the code if(framecount% 60 == 0) print d to print out a clock incrementing every second. The clock prints out every second which means it is working. I am stumped! Please help!
void fireEnemyBullets() {
int xDir = 0;
int yDir = 1;
int xPos;
int yPos;
int bulletType;
if(frameCount % 60 == 0){
println(d);
d++;
}
for (int i = enemies.size() - 1; i >= 0; i--) {
Enemy enemy = (Enemy) enemies.get(i);
ArrayList turrents = enemy.getCurrentTurrentPostions();
for (int j = turrents.size() - 1; j >= 0; j-= 3) {
xPos = (Integer) turrents.get(j - 2);
yPos = (Integer) turrents.get(j - 1);
bulletType = (Integer) turrents.get(j);
if (bulletType == 1 && frameCount % 60 == 0)//Small
{
//println("small fired");
enemyBullets.add(new Bullet(xPos, yPos, xDir, yDir));
}
else if (bulletType == 2 && timePassed2 == 2)//Large
{
println("large fired");
enemyBullets.add(new LargeBullet(xPos, yPos, xDir, yDir));
}
else if (bulletType == 3 && timePassed3 == 2)//Tracking
{
println("tracking fired");
enemyBullets.add(new TrackingBullet(xPos, yPos, playerShip.getTrackingDestX(), playerShip.getTrackingDestY()));
}
else if (bulletType == 4 && timePassed4 == 4)//Drone
{
// print("Spawning drone");
for (int z = 0; z < 15; z++) {
if (!drones[z].isActive()) {
drones[z].setDrone(xPos, yPos, playerShip.getTrackingDestX(), playerShip.getTrackingDestY(), 5);
z = 15;
}
}
}
}
}
}
void fireEnemyBullets() {
int xDir = 0;
int yDir = 1;
int xPos;
int yPos;
int bulletType;
if(frameCount % 60 == 0){
println(d);
d++;
}
for (int i = enemies.size() - 1; i >= 0; i--) {
Enemy enemy = (Enemy) enemies.get(i);
ArrayList turrents = enemy.getCurrentTurrentPostions();
for (int j = turrents.size() - 1; j >= 0; j-= 3) {
xPos = (Integer) turrents.get(j - 2);
yPos = (Integer) turrents.get(j - 1);
bulletType = (Integer) turrents.get(j);
if (bulletType == 1 && frameCount % 60 == 0)//Small
{
//println("small fired");
enemyBullets.add(new Bullet(xPos, yPos, xDir, yDir));
}
else if (bulletType == 2 && timePassed2 == 2)//Large
{
println("large fired");
enemyBullets.add(new LargeBullet(xPos, yPos, xDir, yDir));
}
else if (bulletType == 3 && timePassed3 == 2)//Tracking
{
println("tracking fired");
enemyBullets.add(new TrackingBullet(xPos, yPos, playerShip.getTrackingDestX(), playerShip.getTrackingDestY()));
}
else if (bulletType == 4 && timePassed4 == 4)//Drone
{
// print("Spawning drone");
for (int z = 0; z < 15; z++) {
if (!drones[z].isActive()) {
drones[z].setDrone(xPos, yPos, playerShip.getTrackingDestX(), playerShip.getTrackingDestY(), 5);
z = 15;
}
}
}
}
}
}
1