carobnodrvo
carobnodrvo

Reputation: 1051

Why my FPS drop?

It drops only when object is clicked and needs to be removed from list. Here is code:

if(event.type == TouchEvent.TOUCH_DOWN){
            for(Bottle bottle : new ArrayList<Bottle>(bottles)){
                if(bottle.position.dist(touchPoint) < 40 ){
                    bottles.remove(bottle);
                    if(bottle.type == Bottle.BOTTLE){
                        score+=10;
                        Assets.playSound(Assets.beeropenSound);
                    }
                    else if (bottle.type == Bottle.BOTTLE30){
                        score+=30;
                        Assets.playSound(Assets.beeropenSound);
                    }
                    else if (bottle.type == Bottle.GLASS_OF_BEER){
                        score+=5;
                        Assets.playSound(Assets.pourbeerSound);
                    }
                    else if (bottle.type == Bottle.WATER_BOTTLE){
                        score-=50;
                    }

                    // burping
                    if (score % 200 == 0 && score > 1){
                        Assets.playSound(Assets.burpSounds[burp]);
                    }
                    break;
                }
            }

, and here is fps log:

01-20 19:18:19.629: D/FPS(27501):  59
01-20 19:18:20.639: D/FPS(27501):  59
01-20 19:18:21.639: D/FPS(27501):  49
01-20 19:18:22.649: D/FPS(27501):  59
01-20 19:18:23.669: D/FPS(27501):  60
01-20 19:18:24.669: D/FPS(27501):  59
01-20 19:18:25.689: D/FPS(27501):  60
01-20 19:18:26.699: D/FPS(27501):  43
01-20 19:18:27.719: D/FPS(27501):  60
01-20 19:18:28.739: D/FPS(27501):  60
01-20 19:18:29.759: D/FPS(27501):  60

I tried removing this stuff but it is not it:

One thing is left - garbage collectors. Is it what makes my fps drops ? How can I be sure ? How to fight these enemies ?

And as I said before it ONLY drops when this block of code is executed.

Upvotes: 1

Views: 567

Answers (1)

Johnny S&#248;rensen
Johnny S&#248;rensen

Reputation: 209

I think it's the GC as well, I would add a flag on the bottles list that tells the object it is "removed" instead of actually removing it, thereby avoiding the gc.

Edit: Why are you creating a new arraylist on every touch? shouldn't it just be:

for( Bottle bottle : bottles ) {...}

Upvotes: 1

Related Questions