2013-07-09 207 views
2

我是Box2D的新手。我只是想跟隨一個簡單的教程,而是試圖將其整合我的代碼裏面:Box2D Libgdx黑色屏幕

我開始創建我的Libgdx遊戲:

public void create() { 
    float w = Gdx.graphics.getWidth(); 
    float h = Gdx.graphics.getHeight(); 
    camera = new OrthographicCamera(w, h, 0); 
    camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f); 
    camera.update(); 
    batch = new SpriteBatch(); 
    viewSwitcher("GameScreen",null); 
    } 

到viewSwitcher的調用,創建一個新的對象,它創建了一個新畫面:

public GameScreenController(SomeGame t, String id) { 
    somegame = t; 
    db = t.getDB(); 
    world = new World(new Vector2(0, -20), true); 
    screen = new GameScreen(this); 
    [. . .] 
} 

裏面的遊戲屏幕(它擴展了Screen類)我有渲染方法:

@Override 
public void render(float delta) { 
    debugRenderer.render(world, camera.combined); 
    world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS); 
    stage.act(delta); 
    //stage.draw(); 
} 

最後,一個定時器,定期創建新的對象。這些對象的構造函數中,我有bodyDef的創建:

public void createBodyLetter() { 
    BodyDef bodyDef = new BodyDef(); 
    bodyDef.type = BodyType.DynamicBody; 
    bodyDef.position.set(200, 200); 
    Body body = controller.getWorld().createBody(bodyDef); 

    PolygonShape dynamicBox = new PolygonShape(); 
    dynamicBox.setAsBox(1.0f, 1.0f); 

    FixtureDef fixtureDef = new FixtureDef(); 
    fixtureDef.shape = dynamicBox; 
    fixtureDef.density = 1.0f; 
    fixtureDef.friction = 0.3f; 
    body.createFixture(fixtureDef); 

    dynamicBox.dispose(); 

} 

結果當我開始這個計劃只是一個黑色的屏幕。有誰知道問題在哪裏?

謝謝

+0

也許,取消註釋'//stage.draw();':) – Pavel

+0

是的,我知道!但我想解決的問題與繪製舞臺無關:D! –

+0

javaw.exe出現錯誤嗎? –

回答

0

初始化相機

camera = new OrthographicCamera(w, h); 

你給第三個參數是會導致問題爲您

檢查此鏈接 http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/OrthographicCamera.html#OrthographicCamera(float上的Javadoc鑽石角,浮動,浮動)

+1

好吧,現在好多了!謝謝! 我也讀過,我不能簡單地添加動態的新身體,這個操作只能在踏入世界之前完成:添加身體 - >走向世界。你確認嗎? –

+0

是的,你可以在踏入世界之前或之後添加身體。你不能在步驟週期中添加物體,它會給你一個本地錯誤。 –