如何使用libgdx绘制直线?

如何使用libgdx绘制直线?,libgdx,Libgdx,我正在尝试使用libgdx进行调试,但失败了。这是我的密码 public类主菜单扩展屏幕实现InputProcessor{ 私人正交摄影机; SpriteBatch myBatch; 整形器整形器; 公共主菜单(游戏){ 超级(游戏); camera=新的正交摄影机(Gdx.graphics.getWidth(),Gdx.graphics.getHeight()); setToOrTo(true,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

我正在尝试使用libgdx进行调试,但失败了。这是我的密码

public类主菜单扩展屏幕实现InputProcessor{
私人正交摄影机;
SpriteBatch myBatch;
整形器整形器;
公共主菜单(游戏){
超级(游戏);
camera=新的正交摄影机(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
setToOrTo(true,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
camera.update();}
@凌驾
公共无效渲染(浮动增量){
glClearColor(0,0,0,1);
Gdx.gl.glClear(GL10.gl\u颜色\u缓冲\u位);
myBatch.begin();
stage.draw();
glx.gl10.glLineWidth(10);
ShapedBugger.setProjectionMatrix(camera.combined);
shapeDebugger.begin(ShapeType.Line);
setColor(1,1,1,1);
形状缺陷线(2,2,5,5);
myBatch.end();
}
}
我收到一条错误的线路

ShapedBugger.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();

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

您一定得到了
nullpointerException
,因为您没有创建任何ShaperEnder对象。 在构造函数中插入此行

shapeDebugger=new ShapeRenderer();

请记住,使用SpriteBatch嵌套Shapender可能会导致问题


检查这个。

我的答案对你来说可能太晚了,但对那些有同样定位问题的人来说

关于调整大小后的位置的第二个问题是,视口没有更改。 Aldo您的窗口大小已更改您的应用程序仍然使用与函数
camera.setToOrtho创建时设置的像素大小相同的像素大小

在调整大小时更新视图端口

//-----------------------------------------------------------------
@Override
public void resize (int width, int height) {
    camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.update();        
}

定义并初始化ShaperEnder

ShapeRenderer shapeDebugger;

@Override
public void create() {
    shapeDebugger = new ShapeRenderer();
 ...
在渲染回调中绘制线

    @Override
    public void render() {
        //render scene

        Gdx.gl.glClearColor(69f / 255, 90f / 255, 100f / 255, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    ... 
   shapeDebugger.setProjectionMatrix(camera.combined);
        shapeDebugger.begin(ShapeRenderer.ShapeType.Line);
        Gdx.gl.glLineWidth(10 / camera.zoom);
        shapeDebugger.setColor(1, 0, 0, 1);
        shapeDebugger.line(screenWidth / 2, 0, screenWidth / 2, screenHeight);
        shapeDebugger.line(0, screenHeight / 2, screenWidth, screenHeight / 2);
        shapeDebugger.end();

你犯了什么错误?它是运行时还是编译时?我遇到的一个问题是,在使用Gdx.graphics.getWidth()和Gdx.graphics.getHeight()的bcoz的大小上画一条线来居中。使用你的平截头宽度和平截头高度,你的问题就会得到解决谢谢,我只想测试这条线,因为主要问题是我的纹理没有像预期的那样到达中心