【问题标题】:libGDX Tiled Map hiding sprites that are out of viewportlibGDX Tiled Map 隐藏视口外的精灵
【发布时间】:2018-08-27 10:51:50
【问题描述】:

我开始使用 libGDX 和 Tiled 作为地图创建者创建 2D 游戏。我正在使用一些精灵作为 Tiled 中的图像集合。

问题是每当我向右移动并且某些精灵的左下角超出视口时,它就会像这样消失:

左边应该是一堵墙,一部分天花板和一部分地板,但当我向右移动时它消失了。

这是我的代码:

public class Main implements ApplicationListener {
    private static final int VIEWPORT_WIDTH = 800;
    private static final int VIEWPORT_HEIGHT = 480;

    private TiledMap tiledMap;
    private TiledMapRenderer tiledMapRenderer;
    private OrthographicCamera camera;

    private SpriteBatch batch;

    private Texture playerImage;
    private Rectangle playerRect;

    @Override
    public void create() {
        this.camera = new OrthographicCamera();
        this.camera.setToOrtho(false, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
        this.camera.update();

        this.tiledMap = new TmxMapLoader().load("levels/demo_4x.tmx");
        this.tiledMapRenderer = new OrthogonalTiledMapRenderer(this.tiledMap);

        this.batch = new SpriteBatch();
        this.font = new BitmapFont();

        this.playerImage = new Texture(Gdx.files.internal("person-demo.gif"));
        this.playerRect = new Rectangle();
        this.playerRect.x = 276;
        this.playerRect.y = 88;
        this.playerRect.width = 128;
        this.playerRect.height = 128;
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glBlendFunc(GL30.GL_SRC_ALPHA, GL30.GL_ONE_MINUS_SRC_ALPHA);
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

        this.camera.position.x = this.playerRect.x + (this.playerRect.width / 2);
        this.camera.position.y = this.playerRect.y + (this.playerRect.height / 2);
        this.camera.update();
        this.tiledMapRenderer.setView(this.camera);
        this.tiledMapRenderer.render();

        this.batch.begin();

        this.batch.draw(this.playerImage, this.playerRect.x, this.playerRect.y, this.playerRect.width, this.playerRect.height);

        this.batch.end();
        this.batch.setProjectionMatrix(this.camera.combined);
    }
}

我一整天都想不出如何解决这个问题。我希望有人能够做到。

【问题讨论】:

    标签: java libgdx sprite tiled tmx


    【解决方案1】:

    你可以在设置tiledMapRenderer.setView(cam);时添加一个off set

    而不是那样做

    float width = cam.viewportWidth *cam.zoom;
    float height = cam.viewportHeight * cam.zoom;
    
    float w = width * Math.abs(cam.up.y) + height * Math.abs(cam.up.x);
    float h = height * Math.abs(cam.up.y) + width * Math.abs(cam.up.x);
    float x = cam.position.x - w / 2;
    float y = cam.position.y - h / 2;
    
    x -= offset;
    w += offset;
    tiledMapRenderer.setView(cam.combined,x,y,w,h);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-18
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多