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 !