0
\$\begingroup\$

Well, in my game while I'm drawing I have to verify the collision between the views. After a collision, I have to draw some things. But I can't draw them with a batch but with a stage. So I have to begin and end a lot of batches and this makes my game very slow.

Here is the part of code which I talked about:

for(int i=0;i<1000;i++)
    {
        if(i<999)
            list.get(i+1).xTubo = list.get(i).xTubo + list.get(i).distanza;

        for(int j=0;j<=60;j++)
        {
            if( (xPersonaggio==list.get(i).xTubo + j || xPersonaggio==list.get(i).xTubo-j*3)&& yPersonaggio<=list.get(i).altezza-50)
            {
                batch.begin();
                if(ySalto==0)
                    batch.draw((TextureRegion) animation.getKeyFrame(time, true), orthographicCamera.position.x - 300, 60 + ySalto);
                else
                    batch.draw(textureAtlas.createSprite("h"), orthographicCamera.position.x - 300, 60 + ySalto);
                scrittaHaiPerso.draw(batch,"HAI PERSO!",orthographicCamera.position.x-50,500);
                menu.draw(batch,"MENU",orthographicCamera.position.x,300);
                batch.end();
                stage.draw();

                fineGioco=true;
            }
        }
        batch.begin();
        batch.draw(spriteTuboVerde,list.get(i).xTubo,-30,120,list.get(i).altezza);
        batch.end();
        if(xPersonaggio>list.get(i).xTubo+50)
            tubiSaltati++;
    }

In this cycle I draw a lot of views, as you can see at the bottom. At the top I have my very rudimental collision system. When a collision happens, an imagebutton appears on the screen and I have to draw it with a stage. So I have to begin and ends a lot of times the batch and this doesn't go well at all. How can I write less batches?

NOTE: The order of drawing has to be this

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Generally your collision should be handled separately from your rendering. In your update you should be having your object manager checking collision and handling flags inside said objects to mark whether the objects should be rendered or not based on their interactions. The render should be handling just the rendering portion. Ie. if the render flag of this object is set, render it. It doesn't have to know anything else besides that and you should be able to just wrap all the renders in one single begin and end.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.