简体   繁体   English

如何在Libgdx中设置Screen的宽度和高度?

[英]How to set the width and height of Screen in Libgdx?

Sorry, I can't describe my problem clearly. 对不起,我无法清楚地描述我的问题。 I re-describe it: 我重新描述它:

  1. I am making an android game with libgdx, and for now I finish a demo: 我正在用libgdx制作一个安卓游戏,现在我完成了一个演示:

演示

It has only one game view which is implemented by GameScreen class which is a subclass of Screen in libgdx: 它只有一个游戏视图,由GameScreen类实现,它是libgdx中Screen的子类:

public class LaohuGame extends Game {

public static final int FRUIT_NUM = 24;
public static final String WORK_DIR = "Desktop/assets/";

@Override
public void create() {
    GameScreen gameScreen = new GameScreen(this);
    setScreen(gameScreen);

}

public class GameScreen extends LaohuScreen {

private final Stage stage;
public static int START_X = 11;
public static int START_Y = 199;

public GameScreen(Game game) {
    super(game);
    stage = new Stage(GAME_WIDTH,GAME_HEIGHT,true);
    //add actors to the stage
}

public class LaohuScreen implements Screen{

int GAME_WIDTH;
int GAME_HEIGHT;
protected Game game;
public LaohuScreen(Game game) {
    this.game = game;
    GAME_WIDTH = Gdx.graphics.getWidth();
    GAME_HEIGHT = Gdx.graphics.getHeight();
}

The GAME_WIDTH,GAME_HEIGHT is equal to the width,height of phone screen, so the game view fill the whole phone screen. GAME_WIDTH,GAME_HEIGHT等于手机屏幕的宽度,高度,因此游戏视图填满整个手机屏幕。

  1. Now I go on developing my game. 现在我继续开发我的游戏。 I wanna have 4 game view in my game. 我想在我的游戏中有4个游戏视图。 Each game view implemented by a subclass of Screen in libgdx. 每个游戏视图由libgdx中Screen的子类实现。 I think the completed game should look like this pic: 我认为完成的游戏应该像这样的照片:

完成游戏

A button bar is added to the bottom of the phone screen. 按钮栏添加到手机屏幕的底部。 When I click one of the 4 buttons, the game view switched, but the button bar didn't switch. 当我单击4个按钮中的一个时,游戏视图已切换,但按钮栏未切换。 For example, if player clicks the "Rank" button, game set the view to Rank View(implemented by Rank screen) which shows players' scores, and the buttons still visible: 例如,如果玩家点击“排名”按钮,则游戏将视图设置为排名视图(由排名屏幕实施),其显示玩家的分数,并且按钮仍然可见:

按下排名按钮后

  1. I can think of 2 ways to keep the button bar visible: 我可以想到两种方法来保持按钮栏可见:

    1. Combine button bar and game view into a libgdx's Screen. 将按钮栏和游戏视图组合到libgdx的屏幕中。 I think it is a waste because I need to draw 4 buttons in every screen, totally 16 buttons in 4 Screens. 我认为这是浪费,因为我需要在每个屏幕上绘制4个按钮,在4个屏幕中共有16个按钮。

    2. Split phone screen into 2 areas; 将手机屏幕分为2个区域; game view area and button bar area. 游戏视图区和按钮栏区域。 Game view area implemented by libgdx's Screen and I can switch game view by setScreen method. 由libgdx的Screen实现的游戏视图区域,我可以通过setScreen方法切换游戏视图。 The problem is that libgdx's Screen seems always fills the whole phone screen; 问题是libgdx的屏幕似乎总是填满整个手机屏幕; I can't set the screen's width and height. 我无法设置屏幕的宽度和高度。 I tried using resize(int width, int height) method to cut down the height of screen to leave a blank space for button bar, but it doesn't work. 我尝试使用resize(int width, int height)方法来减少屏幕的高度,为按钮栏留下空白,但它不起作用。

Code: 码:

public class LaohuGame extends Game {

public static final int FRUIT_NUM = 24;
public static final String WORK_DIR = "Desktop/assets/";


@Override
public void create() {
    GameScreen gameScreen = new GameScreen(this);
    setScreen(gameScreen);

}

@Override
public void setScreen(Screen screen) {
    super.setScreen(screen);
    this.getScreen().resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight() * 5 / 10);
}

@Override
public void resize(int width, int height) {
    if (getScreen() != null) getScreen().resize(width, height * 5 / 10);
}
}

Any suggestions are appreciated. 任何建议表示赞赏。

All libgdx things assume that the width and height always are equal to the "window" size. 所有libgdx事物都假定宽度和高度始终等于“窗口”大小。 On a desktop machine this might be less than the whole screensize, but it will still fill the whole render area. 在台式机上,这可能比整个屏幕尺寸小,但它仍将填满整个渲染区域。 On a phone it will most likely always be the phones display size. 在手机上,它很可能永远是手机的显示尺寸。

Setting it to something smaller will not help you, because it will not use only a part of rendering area. 将其设置为较小的东西对您没有帮助,因为它不会仅使用渲染区域的一部分。 Instead you will get weirdly stretched results. 相反,你会得到奇怪的延伸结果。

Don't alter the values you get from the resize method. 不要更改从resize方法获得的值。 Instead go with your first solution and render the buttons on the bottom part of your game screens. 而是使用您的第一个解决方案并渲染游戏屏幕底部的按钮。

You don't need to duplicate any code here. 您无需在此处复制任何代码。 Implement something like a "ButtonBarGUI" which uses a Stage and the scene2d.ui stuff to render four buttons on the bottom of the screen and then just reuse this helper class to render the button bar on any screen via Stage.draw(). 实现类似“ButtonBarGUI”的东西,它使用Stage和scene2d.ui的东西来渲染屏幕底部的四个按钮,然后只需重复使用这个帮助器类通过Stage.draw()在任何屏幕上渲染按钮栏。

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

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