我是 Box2D 的新手。我只是想遵循一个简单的教程,但试图将它集成到我的代码中:
我开始创建我的 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 的调用会创建一个新对象,该对象会创建一个新 Screen:
public GameScreenController(SomeGame t, String id) {
somegame = t;
db = t.getDB();
world = new World(new Vector2(0, -20), true);
screen = new GameScreen(this);
[. . .]
}
在游戏屏幕(扩展屏幕类)内,我有渲染方法:
@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();
}
当我启动程序时,结果只是一个黑屏。有谁知道问题出在哪里?
谢谢