簡體   English   中英

我的帶有libgdx的游戲屏幕在android上縮小

[英]My Game screen with libgdx zoom out on android

我正在學習libgdx(java)游戲編程,剛剛完成了我的第一場比賽:一個簡單的Brick Destroyer。

我做了一個桌面和android項目。 經過數小時的“艱苦”工作,我的游戲終於可以在台式機上正常運行了。 我沒有為此游戲使用scene2D或box2D或其他任何東西(我只是在類上實現了“屏幕”界面:GameScreen,MainMenuScreen等。)

這是問題所在:當我將游戲導出為.APK文件並將其安裝在我的Android手機(Sony Xperia Z1 compact)上時,屏幕絕對不同。 球與其他所有元素(磚塊和屏幕側面)的反應仍然很好,但似乎縮放效果不同。 我已經嘗試了很多東西,但是我不知道該怎么做,請幫助我:)。

我正在使用FitViewport和正交攝影機。

請看下面的屏幕截圖:1-GameScreen類的構造函數。 2-桌面程序。 3-Android應用。

謝謝大家的回答,如果我犯了錯誤(我不是英語為母語的人),也要感謝我的英語。

final Bricks game;

Texture ball_img, plate_img, brique_img_rouge, brique_img_orange, brique_img_vert;  
Vector2 posb, posp;
Vector2 vitb, vitp;
OrthographicCamera camera;
Rectangle ball, plate;
Array<Brique> briques;
FitViewport viewport;
final int plate_width = 60, plate_height = 10;
final int brique_width = 35, brique_height = 15;
final int ball_width = 15, ball_height = 14;
boolean left_p, right_p, up_p;    


public GameScreen(final Bricks gam) {

    this.game = gam;


    ball_img = new Texture("ball_bl.PNG");
    plate_img = new Texture("plate_bl.PNG");
    brique_img_rouge = new Texture("brique_bl_rouge.PNG");
    brique_img_orange = new Texture("brique_bl_orange.PNG");
    brique_img_vert = new Texture("brique_bl_vert.PNG");

    camera = new OrthographicCamera();
    viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera);
    viewport.apply();               

    ball = new Rectangle();
    plate = new Rectangle();

    briques = new Array<Brique>();
    briques.add(new Brique(2));
    briques.first().x = 50;
    briques.first().y = Gdx.graphics.getHeight()-60;
    briques.first().width = brique_width;
    briques.first().height = brique_height;

    posb = new Vector2(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/4);
    vitb =  new Vector2(0, 400*Gdx.graphics.getDeltaTime());

    posp = new Vector2(Gdx.graphics.getWidth()/2 - plate_width/2, 20);
    vitp =  new Vector2(400*Gdx.graphics.getDeltaTime(), 0);

    for (int i = 1; i < 65; i++) {

        if( i % 13 == 0) {
            briques.add(new Brique(1));
            briques.get(i).x = briques.first().x;
            briques.get(i).y = briques.get(i-1).y - 35;
            briques.get(i).width = brique_width;
            briques.get(i).height = brique_height;

        } else {
            briques.add(new Brique(1));
            briques.get(i).x = briques.get(i-1).x + briques.get(i-1).width + 20;
            briques.get(i).y = briques.get(i-1).y;
            briques.get(i).width = brique_width;
            briques.get(i).height = brique_height;
        }

        if(i > 51 || i == 19 || i == 32) {
            briques.get(i).setType(3);
        }
        if((i < 13 || i %13 == 0 || i == 25 || i >= 38) && i < 52) {
            briques.get(i).setType(2);
        }
    }

    ball.x = posb.x;
    ball.y = posb.y;
    ball.width = ball_width;
    ball.height = ball_height;

    plate.x = posp.x;
    plate.y = posp.y;
    plate.width = plate_width;
    plate.height = plate_height;

我認為問題出在這里,但可以在台式機上使用。

camera = new OrthographicCamera();
    viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera);
    viewport.apply();       

桌面: 桌面程序

Android: .APK文件中的android應用

發生這種情況,是因為您使用的虛擬屏幕尺寸為實際屏幕尺寸的FitViweport。 由於您的桌面應用和Android手機具有不同的分辨率,因此您將獲得不同的視口。

因此,對於您的桌面應用程序,攝像頭代碼實際上可能像這樣:

camera = new OrthographicCamera();
viewport = new FitViewport(800, 600, camera);
viewport.apply();

而對於您的android來說,它實際上可能像這樣:

camera = new OrthographicCamera();
viewport = new FitViewport(400, 700, camera);
viewport.apply();

為了解決這個問題,定義了一個內部虛擬世界的大小,因此對於這兩個世界來說,例如都是VIRTUAL_WIDTH x VIRTUAL_HEIGHT 該尺寸不能以像素為單位:

// Global static field in your game class
public static int VIRTUAL_WIDTH = XXX;
public static int VIRTUAL_HEIGHT = XXX;

// ...

camera = new OrthographicCamera();
viewport = new FitViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, camera);
viewport.apply();

這意味着分別使用這些值而不是使用Gdx.graphics.getWidth()Gdx.graphics.getHeight() ,因為您現在使用的是虛擬屏幕尺寸。

例如,在您的代碼中:

posb = new Vector2(VIRTUAL_WIDTH / 2f, VIRTUAL_HEIGHT / 4f);

對於您的屏幕外問題:

檢查如何繪制塊,是否使用Gdx.graphics.getXXX()或超出新大小限制的值?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM