java - CPU usage increases in time -
i making game (asteroids, ship destroys asteroids bullets) , have problem:
if spawn 50 asteroids @ start, cpu usage 2-3%. ok.
if spawn 5 asteroids , play game , moment when there 50 asteroids (increasing each level, when destoy previous), cpu usage becomes 20%
in addition if destroy 49/50 asteroid , leave one alive, cpu usage still 20%, though there nothing compute ( mean moving, collssion detection, etc.)
i have main gameloop timertask
public void startgameloop() { timer.schedule(gamelooptask, 0, 17); timer.schedule(updatescoreandlifestask, 2000, 300); // update score views } class gameloop extends timertask { public void run() { game.updatestate(); openglview.requestrender(); } } public void updatestate() { ship.move(); for(int i=0; i<asteroids.size();i++) // asteroids , ship.bullets arraylists asteroids.get(i).move(); for(int i=0; i<ship.bullets.size();i++) ship.bullets.get(i).move(); detectcollision(); } public void detectcollision() { detectcollisionasteroidswithbullets(); if(ship.isalive && !ship.isimmortal) detectcollisionasteroidswithship(); }
and detectcollision
method loops copy of ship.bullets
, asteroids
arraylists
:
public void detectcollisionasteroidswithbullets() { mainloop: for(int b=0; b < ship.bullets.size(); b++) { for(int a=0; a<asteroids.size(); a++) {
and
public void detectcollisionasteroidswithship() { mainloop: for(int a=0; a<asteroids.size(); a++) { if(isaabboxintersectwithaabbox(asteroids.get(a).aabbcoords, ship.aabbcoords)) {
so shouldn't use cpu hard if there only 1 asteroid. destroying bullets , asteroids = asteroids.remove(asteroid);
(i remove arraylist
please, me, have no idea why keeps using cpu if there loops works prety 50 asteroids , sure shouldn't 20% cpu usage 1 asteroid
are supposed make clones of existing objects? asteroids destroy still there in game after destroy them.
you should remove them or start using them again.
Comments
Post a Comment