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