【问题标题】:Black Screen when using Tiled in LibGDX在 LibGDX 中使用 Tiled 时出现黑屏
【发布时间】:2017-12-15 07:32:56
【问题描述】:

对于我正在制作的游戏,我使用 Tiled 来制作和编辑地图。但是,到目前为止,这些地图并未加载到我制作的示例中:

package com.bluezamx.magillion.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FillViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.bluezamx.magillion.Magillion;
import com.bluezamx.magillion.utils.Constants;

public class WorldScreen implements Screen {

    private Magillion game;
    private OrthographicCamera gameCam;
    private Viewport gamePort;
    private Stage stage;

    //Tiled variables
    private TmxMapLoader mapLoader;
    private TiledMap map;
    private OrthogonalTiledMapRenderer maprenderer;

    // Box2D variables
    private World world;
    private Box2DDebugRenderer debug;

    public WorldScreen (Magillion game) {
        this.game = game;
        gameCam = new OrthographicCamera();

        mapLoader = new TmxMapLoader();
        map = mapLoader.load("TestWorld.tmx");
        maprenderer = new OrthogonalTiledMapRenderer(map, 1 / Constants.PPM);

        gamePort = new FillViewport(Constants.WIDTH / Constants.PPM, Constants.HEIGHT / Constants.PPM, gameCam);
        gameCam.position.set((gamePort.getWorldWidth() / 2), (gamePort.getWorldHeight() / 2), 0);

        world = new World(new Vector2(0,0), true);
        debug = new Box2DDebugRenderer();
        debug.SHAPE_STATIC.set(1, 0, 0, 1);

        for(MapObject object : map.getLayers().get(1).getObjects().getByType(RectangleMapObject.class)) {
            Rectangle rect = ((RectangleMapObject) object).getRectangle();

            bdef.type = BodyDef.BodyType.StaticBody;
            bdef.position.set(rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2);

            body = world.createBody(bdef);

            shape.setAsBox(rect.getWidth() / 2, rect.getHeight() / 2);
            fdef.shape = shape;
            body.createFixture(fdef);
        }
   }

    @Override
    public void show() {}

    private void update(float dt) {
        handleInput(dt);

        world.step(1/60f, 6, 2);
        player.update(dt);

        gameCam.update();
        maprenderer.setView(gameCam);
    }

    @Override
    public void render(float dt) {
        update(dt);

        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        debug.render(world, gameCam.combined);
        game.batch.setProjectionMatrix(gameCam.combined);
        maprenderer.render();

        game.batch.begin();
        player.draw(game.batch);
        game.batch.end();
    }

    @Override
    public void resize(int width, int height) {}

    @Override
    public void pause() {}

    @Override
    public void resume() {}

    @Override
    public void hide() {}

    @Override
    public void dispose() {
        map.dispose();
        maprenderer.dispose();
        world.dispose();
        debug.dispose();
    }
}

当我运行我的文件(它会自动将游戏画面设置为此文件)时,我只得到一个黑屏。地图本身不显示。地图为 640 * 480 大,放置在标准的 android/assets 文件夹中。

我尝试用我在网上找到的其他代码运行我的地图,它在那里工作。但是,我无法弄清楚我的问题是什么。

【问题讨论】:

    标签: java libgdx tiled


    【解决方案1】:

    您需要使用设备屏幕的宽度和高度来更新您的视口。

    @Override
    public void resize(int width, int height) {
         // use true here to center the camera
         gamePort.update(width,height,false);
    }
    

    看看this的答案,最近我添加了作为解决方案。

    【讨论】:

      【解决方案2】:

      看起来您的地图实际上已被渲染,但只是您的视口不在地图位置。 尝试使用:

      gamecam.setToOrtho(false, gamePort.getWorldWidth()/2, gamePort.getWorldHeight()/2);
      

      而不是

      gameCam.position.set((gamePort.getWorldWidth() / 2), (gamePort.getWorldHeight() / 2), 0);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-23
        • 1970-01-01
        • 2017-09-18
        • 1970-01-01
        • 2023-03-16
        • 1970-01-01
        • 2017-10-03
        相关资源
        最近更新 更多