0
votes

I am trying to allow the player to click hold then drag and let go which will accomplish drawing a line from where you clicked to where you let go. However I can't seem to figure out how to keep the lines rendered. Everytime you click drag and let go it draws the line correctly then disappears if you try and draw another line. Here is my code for the input listener which gets the position

    @Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    if(button == Buttons.LEFT){
        buttonPositions[0].x = screenX;
        buttonPositions[0].y = screenY;
        projector.unproject(buttonPositions[0]);
    }
    return false;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    if(button == Buttons.LEFT){
        buttonPositions[1].x = screenX;
        buttonPositions[1].y = screenY;
        projector.unproject(buttonPositions[1]);
        lineDraws.add(buttonPositions);
    }
    return false;
}

buttonPositions is an array of Vector2 of size 2 so you can add where you clicked and where you let go then the arraylist lineDraws stores that Vector2 array, then I try to loop through this list and render it like so

    public void render(float delta){
    if(!tutOver){
        startTutorial();
    }



    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    renderer.begin();
    for(int i = 0; i < listener.getPositionsList().size() - 1; i+=2){
        renderer.line(listener.getPositionsList().get(i)[0], listener.getPositionsList().get(i + 1)[1]);

    }
    renderer.end();

}

I am at a loss as to what to do, and the i+=2 is so it will draw the first two coordinates then the next two. Any help is greatly appreciated!

1

1 Answers

1
votes

First of all use Array<> libgdx class instead of ArrayList - it is more convenient.

Your problem seems to be in touchDown and touchUp methods. Every time you are changing values of

    buttonPositions[0].x
    buttonPositions[0].y
    buttonPositions[1].x
    buttonPositions[1].y

so even if you add it to collection you are overriding previous one.

Also I don't think you need Array of Arrays of Vectors - it is pretty complicated structure isn't it? Instead use just Array of Vectors then iterate two-by-two as you are doing now.

    //the listener class
    Array<Vector2> lineDraws = new Array<Vector2>();

    ...

    //touchDown class
    lineDraws.add(new Vector2(screenX, screenY));

    projector.unproject( new Vector2(startX, startY) );

    //consider adding some flag here to prevent two pointers touch (when holding finger, touching with another)

    //touchUp
    projector.unproject( new Vector2(screenX, screenY) );
    lineDraws.add(new Vector2(screenX, screenY));

    ...

    //the screen class render method
    for(int i = 0; i < listener.lineDraws.size - 1; i+=2)
    {
        renderer.line(listener.lineDraws.get(i), listener.lineDraws.get(i + 1));

}