Matt
Matt

Reputation: 1704

ShapeRenderer not drawing new lines added during runtime

TL;DR My ShapeRenderer will render lines, but only sometimes.

I've seen these posts: LibGDX: ShapeRenderer drawing... nothing, Libgdx NESTED ShapeRenderer not drawing lines, and https://stackoverflow.com/questions/28800505/libgdx-shaperenderer-not-drawing-in-loop. I'm having the same issue, but none of them have answers. I'm hoping someone can spot what I am doing wrong

I have two classes, an ApplicationListener A and GestureListener B. In ApplicationListener A, I have a global ArrayList<StarPair> pairs which I instantiate in the create() method of A. A also has an instantiated ShapeRenderer draw. In my render loop, I have the following code:

// Render lines
cam.update();
draw.setProjectionMatrix(cam.combined);
draw.begin(ShapeRenderer.ShapeType.Line);
draw.setColor(Color.GREEN);
for(StarPair pair : pairs)
    draw.line(pair.v1, pair.v2);
draw.end();

Which works fine when I just put random star pair objects into the list from within the create() method.

My desired behavior is to have the list to start empty and to have the touchDown() event from B add a StarPair object to the pairs list, which is then rendered by the draw.

In touchDown() I create a dummy StarPair newPair and add it to pairs with: gazer.pairs.add(newPair);, where gazer is the instance of A that has the ShapeRenderer draw. I can see this being added to pairs when I debug and put a conditional breakpoint within render() that only hits after a breakpoint on gazer.pairs.add(newPair); The list grows from size 0 to size 1. The coordinates in v1 and v2 are as I expect them to be, but a new line never starts rendering on my screen, despite their being a StarPair in the list.

Does anyone have any idea why?

Upvotes: 1

Views: 84

Answers (1)

Matt
Matt

Reputation: 1704

The newPair object that I was adding to the end of the list was being set to new StarPair() on every call to touchDown, but it was declared as a global variable, so the pairs list in my ApplicationListener was getting a reference to an object that was immediately being cleared - hence no line drawing. Making newPair local to touchDown() solved the issue.

TL;DR My mistake, not libGDX

Upvotes: 1

Related Questions