【发布时间】:2017-07-04 00:49:09
【问题描述】:
我正在重温 LibGDX 游戏编程,很遗憾我不得不重新学习我曾经知道的东西。
我目前正在使用平铺地图编辑器制作一个非常简单的大金刚风格关卡。这个关卡总共有大约 20 个矩形。
我在我的主 GameScreen 类中创建了一个 box2d 世界,并有一个 for 循环将矩形对象放入世界和调试器中。
我的问题是只显示了我绘制的底部(也是第一个)矩形。我检查了比例,还放了一个println(),它告诉我对象信息已被解析,所有矩形信息都显示正确(即矩形 x、y、w、h 值),但正如我所说,只有一个矩形显示在调试器上。
我在休息了大约 6 个月后才重新开始编程,所以我希望我错过了一些简单的事情。我已经测试了一些旧项目中的相同代码仍然可以正常工作。
这是我的代码,非常感谢任何帮助。谢谢
public class GameScreen implements Screen {
SpriteBatch spriteBatch;
OrthographicCamera cam;
Viewport v;
TmxMapLoader mapLoader;
TiledMap map;
OrthogonalTiledMapRenderer mapRenderer;
World world;
Box2DDebugRenderer b2dr;
float mapScale = 10f/140f;
public GameScreen(){
spriteBatch = new SpriteBatch();
cam = new OrthographicCamera();
v = new FitViewport(Constants.V_WIDTH, Constants.V_HEIGHT, cam);
cam.setToOrtho(false, v.getWorldWidth(), v.getWorldHeight());
mapLoader = new TmxMapLoader();
map = mapLoader.load("level1.tmx");
mapRenderer = new OrthogonalTiledMapRenderer(map, mapScale);
world = new World(new Vector2(0,-9.8f), true);
b2dr = new Box2DDebugRenderer();
// box2d local variables
BodyDef bdef = new BodyDef();
PolygonShape shape = new PolygonShape();
FixtureDef fdef = new FixtureDef();
Body body;
// create platform object rectangles
for (MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)){
Rectangle rect = ((RectangleMapObject)object).getRectangle();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(rect.getX() + rect.getWidth() / 2 * mapScale, rect.y + rect.getHeight() / 2 * mapScale);
body = world.createBody(bdef);
shape.setAsBox(rect.getWidth() / 2 * mapScale, rect.getHeight() / 2 * mapScale);
fdef.shape = shape;
body.createFixture(fdef);
}
}
@Override
public void show() {
}
@Override
public void render(float delta) {
update(delta);
clearScreen();
draw();
}
public void update(float dt){
mapRenderer.setView(cam);
}
public void clearScreen(){
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
public void draw(){
spriteBatch.setProjectionMatrix(cam.combined);
mapRenderer.render();
b2dr.render(world, cam.combined);
spriteBatch.begin();
spriteBatch.end();
}
@Override
public void resize(int width, int height) {
v.update(width, height);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
spriteBatch.dispose();
}
}
【问题讨论】:
标签: for-loop libgdx iteration collision-detection box2d