2013-07-25 147 views
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(); 
    } 
} 

我从线错误

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

当我尝试调整屏幕大小时,它没有更新到中心,它远离右边。

+0

你得到了什么错误?它是运行时还是编译时? –

回答

5

因为您还没有制作任何ShapeRenderer对象,您必须得到nullpointerException。 将此行插入到您的构造函数中。

shapeDebugger=new ShapeRenderer(); 
+0

非常感谢你 – kifcaliph

+0

有一个问题,我得到的是画线的中心调整大小 – kifcaliph

+0

多数民众赞成bcoz你已经使用Gdx.graphics.getWidth()和Gdx.graphics.getHeight()。用你的平截头体和frustumHeight来代替你的问题并解决你的问题 – Pranav008

3

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

检查这个link

+0

谢谢,我只是希望这行测试作为主要问题是我的纹理不会像预期的那样进入中心 – kifcaliph

1

我的回答对你来说可能太迟了,但对于人们来说有多么相同的定位问题。

有关调整大小后位置的第二个问题是因为视口没有更改。 Aldo您的窗口大小改变了您的应用程序stil使用的是由功能创建的相同像素大小camera.setToOrtho

更新调整大小的视口!

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