LibGdx-ViewPort width and height does not match for some devices

前端 未结 2 1317
日久生厌
日久生厌 2020-12-12 05:36

I am using a Viewport for my libGdx landscape game like this.

public static final int WORLD_WIDTH = 1280;
public static final int WORLD_HEIGHT = 800;

camera         


        
2条回答
  •  被撕碎了的回忆
    2020-12-12 06:22

    Finally I made it work. I am not sure whether this is the correct way to solve such problems. I did it like this;

        camera = new OrthographicCamera();
        if(screenHeight>1300)
        {
    
            Constants.WORLD_WIDTH=1280;
            Constants.WORLD_HEIGHT=960;
    
        }
        else{
            Constants.WORLD_WIDTH=1280;
            Constants.WORLD_HEIGHT=800;
    
        }
         viewPort = new FillViewport(Constants.WORLD_WIDTH, Constants.WORLD_HEIGHT, camera);
         viewPort.apply();
    

    I used a viewPort width of 1280x960 for all greater resolution devices while keeping 1280x800 resolution for all other devices that has screen width less than 1300. While drawing background I used viewport width and height to set the size like this.

    bgSprite=new Sprite(bgTexture);
        bgSprite.setPosition(Constants.BG_X,Constants.BG_Y);
    bgSprite.setSize(game.viewPort.getWorldWidth(),game.viewPort.getWorldHeight());
    

    Not sure this the right way,but doing this solved my problem.

提交回复
热议问题