如何使用libgdx绘制一条线?

kif*_*iph 4 libgdx

我试图绘制一些使用libgdx调试的行,但我失败了.这是我的代码

public class MainMenu extends Screen implements InputProcessor {

private OrthographicCamera camera;
SpriteBatch myBatch;
ShapeRenderer shapeDebugger;

public MainMenu(Game game) {
    super(game);
    camera= new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.update();}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    myBatch.begin();
    stage.draw();
    Gdx.gl10.glLineWidth(10);
    shapeDebugger.setProjectionMatrix(camera.combined);
    shapeDebugger.begin(ShapeType.Line);
    shapeDebugger.setColor(1, 1, 1, 1);
    shapeDebugger.line(2, 2, 5, 5);
    myBatch.end();
    }
}
Run Code Online (Sandbox Code Playgroud)

我从行中得到一个错误

shapeDebugger.setProjectionMatrix(camera.combined);

@ Pranav008

非常感谢你.我没想到我需要启动它.但我有一个真正的问题.我将这条线画到游戏画面的中心.

    Gdx.gl10.glLineWidth(2);
    shapeDebugger.setProjectionMatrix(camera.combined);
    shapeDebugger.begin(ShapeType.Line);
    shapeDebugger.setColor(1, 1, 1, 1);
    shapeDebugger.line(Gdx.graphics.getWidth()/2, 0,Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight());
    shapeDebugger.end();
Run Code Online (Sandbox Code Playgroud)

当我尝试调整屏幕大小时,它不会更新到中心,它会向远处移动.

小智 5

你必须得到,nullpointerException因为你没有使用ShapeRenderer的任何对象.在构造函数中插入此行.

shapeDebugger=new ShapeRenderer();
Run Code Online (Sandbox Code Playgroud)