【问题标题】:Texture size with Libgdx使用 Libgdx 的纹理大小
【发布时间】:2015-01-27 17:02:04
【问题描述】:

这可能是一个愚蠢的问题,但我需要一些帮助/解释。当我在 libgdx 中调整窗口大小时,我无法正确调整纹理大小。我想保留纹理的外观,或者至少在较小的屏幕尺寸(例如在移动设备上)上缩小它们。这是我的代码示例,我的原始窗口大小是 600*700 我尝试了很多东西但没有任何效果:\

你能帮帮我吗?提前致谢。

public class GameScreen implements Screen {

    private World world;
    private int ppxX, ppxY;
    private SpriteBatch batch;
    private OrthographicCamera camera;
    private float cameraX, cameraY;

    public GameScreen(World world) {
        this.world = world;
        camera = new OrthographicCamera();
    }

    public void show() {
        batch = new SpriteBatch();
    }

    @Override
    public void render(float delta) {
        world.update(delta);
        updateCamera();

        ppxX = Gdx.graphics.getWidth() / 600;
        ppxY = Gdx.graphics.getHeight() / 700;

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

        batch.setProjectionMatrix(camera.combined);
        batch.begin();

        batch.draw(world.getTexture(), 0, 0, ppxX, ppxY);

        for (GameElement e : world.getListElements()) {
            e.update(delta);
            batch.draw(e.getTexture(), e.getPositionX()*ppxX, e.getPositionY()*ppxY, e.getWidth()*ppxX , e.getHeight()*ppxY );
        }

        batch.end();
    }

【问题讨论】:

    标签: java android libgdx


    【解决方案1】:

    你走在正确的轨道上,但有几件事你必须改变。

    1. 使用ViewPorts

      this.camera = new OrthographicCamera();
      this.viewport = new FitViewport(WORLD_SIZE_X, WORLD_SIZE_Y, this.camera);
      this.batch = new SpriteBatch();
      this.batch.setProjectionMatrix(this.camera.combined);
      
    2. 正确调整大小。

      @Override
      public void resize(int width, int height) {
          this.viewport.update(width, height);
      }
      
    3. 使用世界单位更新相机和渲染。不要误以为屏幕像素。

      public void render(float delta) {
          this.camera.update();
          this.batch.setProjectionMatrix(this.camera.combined);
      
          this.batch.begin();
      
          // draw using WORLD_SIZE
      
          this.batch.end();
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 2012-07-28
      相关资源
      最近更新 更多