0
\$\begingroup\$

I am currently trying to make a little snake clone as my second project with LibGDX. I want to draw the borders of the screen with a ShapeRenderer. The Shape gets displayed flawlessly when I create/draw it in the render method of my game. I wanted to outsource the code into an own "Border"-Class, to get a single object for collision and graphics, but if I do this, the shape does not get displayed anymore.

Working code before outsourcing to a Border-Class:

public void render(float dt){

...
borderRenderer = new ShapeRenderer();
    borderRenderer.begin(ShapeRenderer.ShapeType.Filled);
    borderRenderer.setProjectionMatrix(gameCam.combined);
    borderRenderer.setColor(1.0f, 0.3f, 0.3f, 1);
    borderRenderer.rect(x , y , width, height);
    borderRenderer.end();
}

ShapeRenderer outsourced, gets not displayed anymore:

public void render(float dt){
...
for (Border border : borders) {
            border.draw();
        }
}

public class Border {
    private ShapeRenderer borderRenderer;
    private OrthographicCamera gameCam;
    private float x, y, width, height;

    public Border(float x, float y, float width, float height, OrthographicCamera gameCam){
        this.gameCam = gameCam;
        this.borderRenderer = new ShapeRenderer();
    }

    public void draw(){
        borderRenderer.begin(ShapeRenderer.ShapeType.Filled);
        borderRenderer.setProjectionMatrix(gameCam.combined);
        borderRenderer.setColor(1.0f, 0.3f, 0.3f, 1);
        borderRenderer.rect(x , y , width, height);
        borderRenderer.end();

    }

}

The border also gets properly initialized in the constructor.

As I was writing this I recognized that using assets for this would probably be a better idea, but I'm still curious. Thanks in advance for your help !

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

In your Border constructor you never assign x, y, width, or height to local variables, which means they all default to 0. Once I added those assignments the rest of the code worked fine for me.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for your response.The values get initialized properly. Maybe I should have made it clearer that this is not the issue. \$\endgroup\$
    – Somsky
    Nov 21, 2016 at 15:06
  • \$\begingroup\$ You were right ! I have no idea how I could have been this blind. Thank you very much. \$\endgroup\$
    – Somsky
    Nov 21, 2016 at 18:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .