List of bullets causing lots of fps problems

I have been developing a space invaders clone. And i have gotten my wave system working. after the previous wave is cleared the game spawns a new one, but the old bullets i had shot did not hit the new enemies.

So i made a list that contains all the bullets that are spawning. It works like a dream for the first 2 to 4 waves, but after that it would start causing big fps drops and i thought it must be because of the list being so huge.

I made a function to clear certain type of bullets from the list(if they are off the screen or dead), but the lag continued to happen. I thought that my process of emptying the list was too complicated, but i tried it with a complete clear of the list every wave, but the lag still is there. If i remove the list completely there is no problems.

Some pictures:
Here are my list clearing functions

My Chrome performance test thingy.(i have no idea how to decipher this data)

I can add more pictures if needed.

I’m by no means an expert here, but I do know that its best to use object pools rather than create and destroy objects within the game loop. Are you using a pool and still seeing this issue?

3 Likes

To piggyback on what @snowbillr said check out this example:

https://labs.phaser.io/edit.html?src=src\pools\bullets.js

I’m currently building a game with a ton of projectiles, both player and enemy, and use pools like the example above. I have no issues.

2 Likes

Thanks for your answer. I did create my bullets with a pool but now i cant seem to get the way im supposed to use overlap with them :S should i be calling the groups children or the Bullet itself?

The group should be just bullets. You call overlap on the group itself.

So for example:

bullets = this.add.group(blah blah blah);
enemies = this.add.group( blah blah);
this.physics.add.overlap(enemies, bullets);

You can run overlap and collision checks against groups or individual objects. So you can also do:

this.physics.add.overlap(player, enemyBullets);

1 Like

Thanks for your reply. edit: i figured out my original problem, but now im stuck at figuring out how to do one of my power ups with the pool. So the power ups idea is to create many little projectiles in a circle. My old code:

This is what my circle shooting thing is looking like at the moment. So it works but again after i’ve shot like 10 circles the game starts lagging horribly.
Here is the code:

Here is Google chrome performance report

That’s adding a new collider each time the player fires.

Call this only once:

this.physics.add.overlap(minis, enemies, hitEnemy, null, this);
1 Like

Thanks for your reply i managed to get my performance problems solved, but now i started testing the game and when i start my second game at some point it tells me this:

I think it has something to do with the pool running out or something…
This is how my pool looks like:

And i think the pool is running out because of this:

Is there a way to return the bullets to the pool? Also i tried to clean the group after the game ends like this:

But that seemed to not work…

What exactly do you mean by start your second game?

And i think the pool is running out because of this:

Is there a way to return the bullets to the pool? Also i tried to clean the group after the game ends like this:

But that seemed to not work…

You don’t want to use destroy when using a pool. When you are done with a pool object just do setVisible(False) and setActive(False).

Making an object inactive tells the scene not to run the objects update() method and also makes it available to the groups .get() method.

I don’t think you should run into an error when running out group children as that is expected behavior by the system. Judging by the fact that the error is only referencing Phaser and not your game it is most likely a bug or something you are doing with the game/scene. This brings me back to my original question about second game.

1 Like

Thanks for your response. I managed to find out that it indeed had nothing to do with the pool and a lot to do with the enemies not being able to fire back after the first game. I had .setVisible and .setActive before but i forgot to also move them where they wont be overlapping with the enemies. (that might have caused by something else too) But thanks for everyones help. I think my game is fully working now!

1 Like

Glad to hear! I ran into the same problem with overlapping with inactive projectiles and fixed it by setting my overlap to do callbacks to see whether the projective was active or not before returning overlap.

Cheers!