在 libGDX 中更改分辨率时如何保持平铺地图的外观

how to maintain the appearance of the tiled map when I change the resolution in libGDX

我已经为我正在编写的 2D 游戏创建了一个测试平铺地图。一切都很好!,但是当我更改分辨率时,相机无法正确适应屏幕。

我有一个玩家精灵和 Tile 地图,我使用 1366x768 的分辨率,你可以看到屏幕适合:

但是当我更改分辨率时,例如 640x480。正如您在这张图片中看到的那样,播放器不适合新的分辨率:

播放器看起来更大,但我想根据新的分辨率适应整个屏幕,包括所有精灵。

我认为凸轮渲染有问题,但我不知道该如何解决。相机跟随玩家移动,一切正常,但我想使屏幕游戏适合所选分辨率。

我会放一些我的代码给你看:

这里是主要代码:

public class codeTiled implements ApplicationListener {

... //Variables.....

public void create() {      
    manager = new AssetManager();
    manager.setLoader(TiledMap.class, new TmxMapLoader());
    manager.load("C:/Users/HOME/Desktop/tilemap/TiledMap/data/maps/test.tmx", TiledMap.class);
    manager.finishLoading();
    map = manager.get("C:/Users/HOME/Desktop/tilemap/TiledMap/data/maps/test.tmx", TiledMap.class);
    batch=new SpriteBatch();
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    camera = new OrthographicCamera(50, 50 * (h / w));

    float unitScale = 1 / 8f;
    renderer = new OrthogonalTiledMapRenderer(map, unitScale);
    player=new playerEx(100, 100, camera);
}

public void render() {      
    handleInput();
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    camera.update();
    renderer.setView(camera);
    renderer.render();
    batch.begin();
    player.render(batch);
    batch.end();
}

private void handleInput() {

    if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)){
        System.exit(0);
    }
     camera.zoom = MathUtils.clamp(camera.zoom, 0.1f, 100/camera.viewportWidth);
    float effectiveViewportWidth = camera.viewportWidth * camera.zoom;
    float effectiveViewportHeight = camera.viewportHeight * camera.zoom;
    camera.position.x = MathUtils.clamp(camera.position.x, effectiveViewportWidth / 2f, 100 - effectiveViewportWidth / 2f);
    camera.position.y = MathUtils.clamp(camera.position.y, effectiveViewportHeight / 2f, 100 - effectiveViewportHeight / 2f);
}

这是我播放器的一部分 class:

public class playerEx {

...//Variables....

public playerEx(int x, int y, OrthographicCamera camera){
    this.camera=camera;
    recP= new Rectangle();
    recP.height = 64;
    recP.width = 64;
    recP.x = x;
    recP.y = y;

    imagen=new Texture(Gdx.files.internal("C:/Users/HOME/Desktop/tilemap/TiledMap/data/sprites/player/minigunattack.png"));
    imagen2=new Texture(Gdx.files.internal("C:/Users/HOME/Desktop/tilemap/TiledMap/data/sprites/player/minigunstand.png")); 
    TextureRegion[][] tmp=TextureRegion.split(imagen,
            imagen.getWidth()/5,imagen.getHeight());
    imagen1=new Texture(Gdx.files.internal("C:/Users/HOME/Desktop/tilemap/TiledMap/data/sprites/player/feet.png"));
    TextureRegion[][] tmp1=TextureRegion.split(imagen1,
            imagen1.getWidth()/5,imagen1.getHeight());
    movPlayer=new TextureRegion[5];
    movFeet=new TextureRegion[5];

    for(int i=0;i<5;i++){
        movFeet[i]=tmp1[0][i];
    }for(int i=0;i<5;i++){
        movPlayer[i]=tmp[0][i];
    }animationAttack=new Animation(0.08f,movPlayer);
    animationFeet=new Animation(0.10f,movFeet);
    tiempo=0f;                                                                                                                
}

同样,摄像机被编程为跟随玩家并且工作正常。但是当我想将它更改为另一个分辨率时,精灵播放器不适合平铺地图:(.

希望有人能帮我解决这个... 谢谢!

我建议您为您的世界使用视口和一些常量值。

首先,我们以像素为单位定义默认的屏幕宽度和高度。最终屏幕有多大并不重要。 在我的示例中,我说默认屏幕尺寸是:512x256 像素。

其次,我必须决定一米有多少像素。所以如果我说 256 像素是一米,我的视口显示我的世界的 2x1 米。那是非常小的。如果我希望我的视口显示例如 16 米,我可以计算:512 / 16 = Pixel_Per_Meter。在这种情况下 32.

最后,我们创建一个Constants class:

public class Constants {

    public static final float PPM = 32; // PPM = Pixel per Meter
    public static final float MPP = 1 / PPM; // MPP = Meter per Pixel

    public static final int WORLD_PIXEL_WIDTH = 512;
    public static final int WORLD_PIXEL_HEIGHT = 256;
    public static final float WORLD_WIDTH = WORLD_PIXEL_WIDTH / PPM; //in meter
    public static final float WORLD_HEIGHT = WORLD_PIXEL_HEIGHT / PPM; //in meter
}

现在,当您稍后在游戏中看到显示的世界太小或太大时,您可以更改 WORLD_PIXEL_WIDTHWORLD_PIXEL_HEIGHT 以显示更多或更少

现在我们创建 OrthographicCameraFitViewportOrthogonalTiledMapRenderer

Viewport is a very important part of the game. If you will know more about Viewports read the viewport part of and

所以首先创建我们的 OrthographicCamera 'camera' 和我们的 FitViewport 'viewport'

camera = new OrthographicCamera();
viewport = new FitViewport(Constants.WORLD_WIDTH, Constants.WORLD_HEIGHT, camera);
camera.position.set(viewport.getWorldWidth() / 2, viewport.getWorldHeight() / 2, 0); // Differ from your I eat ann Apple 

然后我们的SpriteBatch'batch'和TiledMap'map'

batch = new SpriteBatch();
map = app.getAssets().loadSingleAsset("map/" + level + ".tmx", TiledMap.class);

最后,我们的 OrthogonalTiledMapRenderer 'mapRenderer'

mapRenderer = new OrthogonalTiledMapRenderer(map, Constants.MPP);

渲染我们的世界:

@Override
public void render(float delta) {

    camera.update();

    mapRenderer.setView(camera);
    mapRenderer.render();

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    player.draw(batch);
    batch.end();
}