gpt4 book ai didi

java - Box2D Libgdx黑屏

转载 作者:行者123 更新时间:2023-11-29 09:00:53 26 4
gpt4 key购买 nike

我是 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);
[. . .]
}

在 Game Screen(它扩展了 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();

}

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

谢谢

最佳答案

将相机初始化为

camera = new OrthographicCamera(w, h);

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

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

关于java - Box2D Libgdx黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17557770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com