【问题标题】:Box2D Libgdx black screenBox2D Libgdx 黑屏
【发布时间】:2013-07-07 14:57:15
【问题描述】:

我是 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();

}

我启动程序的结果只是黑屏。有谁知道问题出在哪里?

谢谢

【问题讨论】:

  • 也许吧,取消注释//stage.draw(); :)
  • 是的,我知道!但我要解决的问题与绘制舞台无关:D!
  • javaw.exe是否出现错误?

标签: java box2d libgdx


【解决方案1】:

将相机初始化为

camera = new OrthographicCamera(w, h);

您给出的第三个参数是菱形角度,这会给您带来问题

检查此链接上的 javadoc http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/OrthographicCamera.html#OrthographicCamera(float,浮动,浮动)

【讨论】:

  • 好的,现在好多了!谢谢!我还读到我不能简单地即时添加新的身体,这个操作应该只在踏入世界之前完成:添加身体->踏入世界。你确认了吗?
  • 是的,您可以在踏入世界之前或之后添加身体。你不能在步骤循环中添加实体,这会给你一个原生错误。
猜你喜欢
  • 1970-01-01
  • 2015-01-23
  • 2014-08-05
  • 2015-02-16
  • 1970-01-01
  • 2014-07-02
  • 1970-01-01
  • 1970-01-01
  • 2013-07-05
相关资源
最近更新 更多