简体   繁体   English

我的带有libgdx的游戏屏幕在android上缩小

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

I'm learning libgdx (java) game programming and I just finished my first game : a simple Brick Destroyer. 我正在学习libgdx(java)游戏编程,刚刚完成了我的第一场比赛:一个简单的Brick Destroyer。

I made a desktop and android project. 我做了一个桌面和android项目。 After some hours of "hard" work, my game finally works fine on desktop. 经过数小时的“艰苦”工作,我的游戏终于可以在台式机上正常运行了。 I'm not using scene2D or box2D or anything else for this game (I just implemented the interface "screen" on my classes : GameScreen, MainMenuScreen etc...). 我没有为此游戏使用scene2D或box2D或其他任何东西(我只是在类上实现了“屏幕”界面:GameScreen,MainMenuScreen等。)

Here is the problem : When I export my game as an .APK file and then install it on my android phone (Sony Xperia Z1 compact), the screen is absolutely different. 这是问题所在:当我将游戏导出为.APK文件并将其安装在我的Android手机(Sony Xperia Z1 compact)上时,屏幕绝对不同。 The ball still reacts well with all other elements (bricks and sides of the screen) but it seems not to be the same zoom. 球与其他所有元素(砖块和屏幕侧面)的反应仍然很好,但似乎缩放效果不同。 I've tried so much things, I don't know what to do anymore, please help me :). 我已经尝试了很多东西,但是我不知道该怎么做,请帮助我:)。

I'm using a FitViewport and an Orthographic Camera. 我正在使用FitViewport和正交摄影机。

See below the screenshots : 1 - The constructor of GameScreen class. 请看下面的屏幕截图:1-GameScreen类的构造函数。 2 - Desktop programm. 2-桌面程序。 3 - Android app. 3-Android应用。

Thank you all for your answers and sorry for my english if I made mistakes(I'm not an english native speaker). 谢谢大家的回答,如果我犯了错误(我不是英语为母语的人),也要感谢我的英语。

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;

I think the problem comes from here, but it works on desktop. 我认为问题出在这里,但可以在台式机上使用。

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

Desktop : Desktop programm 桌面: 桌面程序

Android : android app from .APK file Android: .APK文件中的android应用

This happens, since you use a FitViweport with a virtual screen size of the actual screen size. 发生这种情况,是因为您使用的虚拟屏幕尺寸为实际屏幕尺寸的FitViweport。 Since your desktop app and android phone have a different resolution, you get different viewports. 由于您的桌面应用和Android手机具有不同的分辨率,因此您将获得不同的视口。

So for your desktop app the camera code might actually "look" like this: 因此,对于您的桌面应用程序,摄像头代码实际上可能像这样:

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

And for you android it might actually "look" like this: 而对于您的android来说,它实际上可能像这样:

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

To solve that define an inner virtual world size, so that for both the world size is for example VIRTUAL_WIDTH x VIRTUAL_HEIGHT . 为了解决这个问题,定义了一个内部虚拟世界的大小,因此对于这两个世界来说,例如都是VIRTUAL_WIDTH x VIRTUAL_HEIGHT This size must not be in pixels: 该尺寸不能以像素为单位:

// 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();

This means that instead of using Gdx.graphics.getWidth() or Gdx.graphics.getHeight() you use these values, respectively, since you now work in your virtual screen size. 这意味着分别使用这些值而不是使用Gdx.graphics.getWidth()Gdx.graphics.getHeight() ,因为您现在使用的是虚拟屏幕尺寸。

For example here in your code: 例如,在您的代码中:

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

For your out of screen problem: 对于您的屏幕外问题:

Check how you draw your blocks, do you use Gdx.graphics.getXXX() or values that exceed your new size limit? 检查如何绘制块,是否使用Gdx.graphics.getXXX()或超出新大小限制的值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM