我是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,创建一个新对象,创建一个新屏幕:
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();
}
最后,Timer会定期创建新对象。在这些对象的构造函数中,我创建了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 :(得分:0)
将相机初始化为
camera = new OrthographicCamera(w, h);
你给出的第三个参数是钻石角度,它会给你造成问题
检查此链接上的javadoc http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/OrthographicCamera.html#OrthographicCamera(float,浮动,漂浮)