Answer the question
In order to leave comments, you need to log in
Why is the map not loading in libGDX?
Good day.
Let's say I have a game designer friend who decided to send me a map created in the . He sent it to me in archive .
But I can't seem to load the map. The screen is blank.
Here is the code:
public class PlayScreen implements Screen {
private MyGdxGame game;
Texture texture;
private OrthographicCamera gameCam;
private Viewport gamePort;
private Hud hud;
private TmxMapLoader mapLoader; // for downloading tiles
private TiledMap map;
private OrthogonalTiledMapRenderer render;
private World world;
private Box2DDebugRenderer b2dr;
private Player player;
// light
private PointLight light;
private RayHandler rayHandlerh;
public PlayScreen(MyGdxGame game) {
this.game = game;
texture = new Texture("badlogic.jpg");
gameCam = new OrthographicCamera();
gamePort = new FitViewport(
MyGdxGame.V_WIDTH / MyGdxGame.PPM,
MyGdxGame.V_HEIGHT / MyGdxGame.PPM, gameCam);
hud = new Hud(game.batch);
// load map
mapLoader = new TmxMapLoader();
map = mapLoader.load("maps/videns.tmx");
render = new OrthogonalTiledMapRenderer(map, 1 / MyGdxGame.PPM);
gameCam.position.set(
gamePort.getWorldWidth() / 2,
gamePort.getWorldHeight() / 2,
2);
// physics and polygon system
world = new World(new Vector2(0, -10), true);
b2dr = new Box2DDebugRenderer();
BodyDef bdef = new BodyDef();
PolygonShape shape = new PolygonShape();
FixtureDef fdef = new FixtureDef();
Body body;
// create ground bodies/fixtures (polygon)
/* for (MapObject obj :
map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)) {
Rectangle rect = ((RectangleMapObject) obj).getRectangle();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(
(rect.getX() + rect.getWidth() / 2) / MyGdxGame.PPM,
(rect.getY() + rect.getHeight() / 2) / MyGdxGame.PPM);
body = world.createBody(bdef);
shape.setAsBox(rect.getWidth() / 2 / MyGdxGame.PPM, rect.getHeight() / 2 / MyGdxGame.PPM);
fdef.shape = shape;
body.createFixture(fdef);
}
// create pipe bodies
for (MapObject obj :
map.getLayers().get(3).getObjects().getByType(RectangleMapObject.class)) {
Rectangle rect = ((RectangleMapObject) obj).getRectangle();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(
(rect.getX() + rect.getWidth() / 2) / MyGdxGame.PPM,
(rect.getY() + rect.getHeight() / 2) / MyGdxGame.PPM);
body = world.createBody(bdef);
shape.setAsBox(rect.getWidth() / 2 / MyGdxGame.PPM, rect.getHeight() / 2 / MyGdxGame.PPM);
fdef.shape = shape;
body.createFixture(fdef);
}
// create bricks
for (MapObject obj :
map.getLayers().get(5).getObjects().getByType(RectangleMapObject.class)) {
Rectangle rect = ((RectangleMapObject) obj).getRectangle();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(
(rect.getX() + rect.getWidth() / 2) / MyGdxGame.PPM,
(rect.getY() + rect.getHeight() / 2) / MyGdxGame.PPM);
body = world.createBody(bdef);
shape.setAsBox(rect.getWidth() / 2 / MyGdxGame.PPM, rect.getHeight() / 2 / MyGdxGame.PPM);
fdef.shape = shape;
body.createFixture(fdef);
}
// create coins
for (MapObject obj :
map.getLayers().get(4).getObjects().getByType(RectangleMapObject.class)) {
Rectangle rect = ((RectangleMapObject) obj).getRectangle();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(
(rect.getX() + rect.getWidth() / 2) / MyGdxGame.PPM,
(rect.getY() + rect.getHeight() / 2) / MyGdxGame.PPM);
body = world.createBody(bdef);
shape.setAsBox(rect.getWidth() / 2 / MyGdxGame.PPM, rect.getHeight() / 2 / MyGdxGame.PPM);
fdef.shape = shape;
body.createFixture(fdef);
}*/
player = new Player(world); // create player
}
public void handleInput(float dt) {
// jumping
if (Gdx.input.isKeyJustPressed(Input.Keys.UP))
player.b2body.applyLinearImpulse(
new Vector2(0, 4f),
player.b2body.getWorldCenter(),
true);
// turn right
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) &&
player.b2body.getLinearVelocity().x <= 2)
player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true);
// turn left
if (Gdx.input.isKeyPressed(Input.Keys.LEFT) &&
player.b2body.getLinearVelocity().x >= -2)
player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true);
}
public void update(float dt) {
handleInput(dt);
world.step(1 / 60f, 6, 2);
// change camera position
gameCam.position.x = player.b2body.getPosition().x;
gameCam.update();
render.setView(gameCam);
}
@Override
public void show() {
}
@Override
public void render(float delta) {
//
update(delta);
// clear game screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// render game map
render.render();
// render box2dbodies
b2dr.render(world, gameCam.combined);
// set batch to draw camera
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
}
@Override
public void resize(int width, int height) {
gamePort.update(width, height);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
}
Answer the question
In order to leave comments, you need to log in
well, they write to you that something is wrong with access to the array - going beyond the bounds, the index cannot be >= size, and you get size 3, you need to look at the code
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question