5

我正在使用 libgdx 制作一个简单的游戏,我想使用 scene2Dui 和舞台和皮肤来简化我的很多定位/调整代码。

为了进一步简化所有内容的定位,我还想定义一个自定义坐标系,以便无论屏幕大小(以像素为单位)如何,代码都会自动创建网格叠加层并正确定位/调整我的所有元素。

我遇到的问题与这个新的坐标系以及它如何与皮肤一起工作有关。基本上,我根据皮肤中的样式创建一个按钮,并将其放在一个表格中,以根据我的坐标调整大小。我还创建了一个图像,但这次直接加载该区域并自行定位。

调试行显示按钮本身工作正常,并且在点击方面表现如预期;如果我在表外单击,尽管图像被炸毁,但没有任何反应。调整窗口大小也会按预期运行。

在某个地方,舞台必须询问皮肤按钮的图片有多大,它以像素为单位回答,它应该在我的坐标中回答。有没有办法将皮肤缩小到我的坐标,并在调整窗口大小时更新它?

以下是相关代码:

// *** Layout Coordinates ***//
// Use the pixels per unit to define a grid and orient
// all the elements of the screen based on that grid.
private static final float CAMERA_WIDTH = 5f; // how many boxes wide the screen is
private static final float CAMERA_HEIGHT = 8f; // how many boxes high the screen is

// Button in the table, all in my coordinate system
private static final float BUTTON_HEIGHT = 1.0f;
private static final float BUTTON_WIDTH = 3.0f;
private static final float TABLE_PADDING_LEFT = 1.0f;
private static final float TABLE_PADDING_TOP = 1.0f;

// Floating image, all in my coordinate system
private static final float IMAGE_HEIGHT = 4.0f;
private static final float IMAGE_WIDTH = 3.0f;
private static final float IMAGE_ORIGIN_X = 1.0f;
private static final float IMAGE_ORIGIN_Y = 1.0f;

private TextButton myButton;

private Image myImage;

private final Stage myStage;
private final Skin mySkin;

private int width;
private int height;

public LayoutTestScreen() {
    width = Gdx.graphics.getWidth();
    height = Gdx.graphics.getHeight();
    Gdx.app.log("SIZE_DEBUG", "Constructing to "+ width + "x" + height);
    myStage = new Stage(width, height, true, game.getSpriteBatch());
    mySkin = new Skin(Gdx.files.internal("uiskin.json"));
}

@Override
public void resize(int width, int height) {
    Gdx.app.log("SIZE_DEBUG", "Resizing to "+ width + "x" + height);
    this.width = width;
    this.height = height;
    myStage.setViewport(CAMERA_WIDTH, CAMERA_HEIGHT, false, 0, 0, this.width, this.height);
    scaleSkinToMatchScreen();
}

@Override
public void show() {
    super.show();

    scaleSkinToMatchScreen();
    buildButtonTable();
    buildImage();

    Gdx.input.setInputProcessor(myStage);
}

public void scaleSkinToMatchScreen(){
    //TODO: Use CAMERA_HEIGHT / this.height and CAMERA_WIDTH / this.width to scale
    //the skin's images down (or up!) to match our coordinate system.
}

private void buildButtonTable() {
    // Adds a table of action buttons in the top-left corner of the screen
    Table buttonTable = new Table(mySkin);

    //Set cell defaults and position the table
    buttonTable.defaults().width(BUTTON_WIDTH);
    buttonTable.defaults().height(BUTTON_HEIGHT);
    buttonTable.top().left();
    buttonTable.padLeft(TABLE_PADDING_LEFT).padTop(TABLE_PADDING_TOP);

    //Add a TextButton to the table
    myButton = new TextButton("button", mySkin);
    myButton.setColor(Color.RED);
    myButton.addListener(new ChangeListener() {
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            Gdx.app.log("BUTTON","Button pressed!");
        }
    });
    buttonTable.add(myButton);
    buttonTable.row();

    //Add the table to the stage
    buttonTable.setFillParent(true);
    buttonTable.debug();
    myStage.addActor(buttonTable);
}

private void buildImage(){
    // Create a new image, size and position it, and then put it in the stage.
    myImage = new Image();
    myImage.setX(IMAGE_ORIGIN_X);
    myImage.setY(IMAGE_ORIGIN_Y);
    myImage.setWidth(IMAGE_WIDTH);
    myImage.setHeight(IMAGE_HEIGHT);
    myImage.setDrawable(new TextureRegionDrawable(new TextureAtlas("images/cards/CardImages.pack").findRegion("b1fv")));
    myStage.addActor(myImage);
}

@Override
public void render(float delta) {
    super.render(delta);    
    myStage.draw();
    myStage.act(delta);
    Table.drawDebug(myStage);
}

这就是它的样子

我在 scaleSkinToMatchScreen() 中放了什么来将所有皮肤的图片缩放到我的坐标?

4

1 回答 1

0

Yeah the button uses a ninepatch. You can set it up by defining the style (including fonts) specifically in the JSON and referring to a pack file of a texture atlas. The scene2d ui libraries are finicky at the best of times, so it's going to take some playing around to get things to work properly. I'd also recommend using hot-code swap to get your positioning and such done right. It's far faster than rebooting your application for every change.

Hot code swap (assuming you're using eclipse, can't speak for other IDEs):

  1. Run your program in debug mode.
  2. Make changes in the IDE code editor
  3. Save your changes
  4. Changes are swapped in and should show up in your application.

You can also use this tool:

https://github.com/cobolfoo/gdx-skineditor

It's very early on, in terms of its development, but it does work quite nicely once you sort out the bugs and weirdness.

It also goes without saying that your libGDX should be updated to the latest build. It is open source, and bugs do pop up from time to time.

于 2014-10-26T16:39:11.427 回答