0
\$\begingroup\$

I am using the LibGDX library to render a 3D model in my Android application. Now, I am stuck, where I want to render multiple dynamic texts on to different 3D boxes located on different coordinates of the screen. I am able to render a single text using SpriteBatch, but I need to render different string values on different boxes.

SpriteBatch spriteBatch = new SpriteBatch();
BitmapFont font = new BitmapFont();
spriteBatch.maxSpritesInBatch = 5;

spriteBatch.setProjectionMatrix(camera.combined.cpy().scale(camera.combined.cpy()
    .getScaleX()/25,camera.combined.cpy().getScaleY()/25, 0.8f));

spriteBatch.begin();

font.setColor(1.0f, 0.0f, 0.0f, 1.0f);
float xPos = -90;
int count = 0;

for (Widget widgets : display.getWidgets()) 
{
    float boxWidth = widgets.getBounds().getWidth();
    float boxHeight = widgets.getBounds().getHeight();
    float boxHeightCenter = widgets.getBounds().getCenterY();

    if(count==0) 
    {
        font.draw(spriteBatch, "Hello"+count, xPos, boxHeightCenter-25, 
            boxWidth, 0, true);
        ++count;
    }
    else
    {
        font.draw(spriteBatch, "Hello"+count, xPos+boxWidth, boxHeightCenter-25, 
            boxWidth, 0, true);
        xPos = xPos+boxWidth;
        ++count;
    }
}

spriteBatch.end();` 

The above code renders only one string value. While debugging, the "for" loop is getting traversed, but no positive result is obtained. How do I fix this?

\$\endgroup\$
0
\$\begingroup\$
   for (Widget widgets :
            display.getWidgets()) {


        for (int i = 0; i < widgets.nodes.size; i++) {

            Node node = widgets.nodes.get(i);
            if(node.id.equalsIgnoreCase("Camera") || node.id.equalsIgnoreCase("lamp")){

}
                else {
                BoundingBox nodeBox = new BoundingBox();
                final Vector3 center = new Vector3();

                node.calculateBoundingBox(nodeBox);
                nodeBox.getCenter(center);
                if(count==0) {
                    font.draw(spriteBatch, "Hello"+count, center.x
                            , center.y, nodeBox.getWidth()*25, Align.center, true);

                    ++count;

                }

                else{


                    font.draw(spriteBatch, "Hello"+count,center.x, center.y, nodeBox.getWidth()*25,Align.center, true);
                    xPos = xPos+(boxWidth*2);
                    ++count;

                }
            }
        }


    }
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Could you describe what the code does? What was your error in the first place? What's your train of thought in fixing it? \$\endgroup\$ – Vaillancourt Mar 20 '17 at 11:03
  • \$\begingroup\$ @AlexandreVaillancourt Initially i was not able to render multiple texts based on the number of nodes present in model. I wanted to render multiple text at different position so i achieved with above mentioned code. There was no error , but i was not able to achieve what i wanted i.e. rendering multiple text. \$\endgroup\$ – Ankit Gupta Mar 21 '17 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.