我是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();
}
結果當我開始這個計劃只是一個黑色的屏幕。有誰知道問題在哪裏?
謝謝
也許,取消註釋'//stage.draw();':) – Pavel
是的,我知道!但我想解決的問題與繪製舞臺無關:D! –
javaw.exe出現錯誤嗎? –