Android Array Lag?
- by Mike
I am making a platform game for Android. It is sort of a tile based game. I added bullets and enemies with AI and a bunch of tile types.
I created a simple map with no Enemies. Everything was running well and smooth until I shot a bunch of bullets randomly everywhere. A couple of hundreds of bullets later, the FPS lowered.
I made a test to find out if the bullets were the problem so I made another simple map with just a tile to stand on and left it for a while. Minutes later, I played around with it a bit to check if the FPS changed and it didnt. I reloaded the same map and shot a lot of bullets. Minutes later, the FPS was visibly lower even after the number of bullets were zero.
Points to note:
Programmed FPS is 30
Tested on a Samsung Galaxy Y and Samsung Galaxy W
Any tile, enemy, bullet that is off screen is not drawn to prevent lag
Bullets collide with Tiles (if they dont collide with in 450 frames, they are removed from the array)
I used List bullets = new ListArray();
I used bullets.add(new Bullet(x, y, params...));
I used for(...){ if(...){ bullets.remove(i); } }
Code for bullet:
private void drawBullets(Canvas canvas) {
for (int i = 0; i < bullets.size(); i++) {
Bullet b = bullets.get(i);
b.update(canvas); //updates physics
if (b.t > blm) { //if the bullet is past its expiry
bullets.remove(i);
i--;
} else {
if (svx((b.x)) > 0 && svx(b.x) < width && svy((b.y)) > 0
&& svy(b.y) < height) { // if bullet is not off screen
b.draw(canvas); // draw the bullet
}
}
}
}
I tried searching for solutions and references but I have no luck. I'm guessing that the lag has something to do with the Array and the Bullets or Classes that I've loaded? I'm not sure!
Someone please help!
Thanks in advance! :)