【问题标题】:LibGDX: Skin + Custom Coordinates Draw IncorrectlyLibGDX:皮肤+自定义坐标绘制不正确
【发布时间】:2014-04-23 03:23:38
【问题描述】:

我正在使用 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);
}

And here's what it looks like

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

【问题讨论】:

  • 更新:我尝试渲染的按钮使用 NinePatch,如果我将所有填充/分割设置为 0,那么图片在框内渲染得很好,但字体仍然混乱且巨大。描述如何缩放 NinePatch 图像以与我在舞台上设置的网格相结合的像素区域似乎没有充分的理由。鉴于我的图片渲染良好(即 1 个像素!= 1 个单位),我不明白为什么按钮应该有任何不同...

标签: java libgdx coordinates scene2d


【解决方案1】:

是的,按钮使用了 Ninepatch。您可以通过在 JSON 中专门定义样式(包括字体)并引用纹理图集的包文件来设置它。在最好的情况下,scene2d ui 库是很挑剔的,所以需要一些时间才能让事情正常工作。我还建议使用热代码交换来获得您的定位并正确完成。它比每次更改都重新启动应用程序要快得多。

热代码交换(假设您使用的是 eclipse,不能代表其他 IDE):

  1. 在调试模式下运行您的程序。
  2. 在 IDE 代码编辑器中进行更改
  3. 保存更改
  4. 更改已换入并应显示在您的应用程序中。

你也可以使用这个工具:

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

就其开发而言,它还处于早期阶段,但是一旦您解决了错误和怪异,它确实可以很好地工作。

不用说你的 libGDX 应该更新到最新版本。它是开源的,并且不时会弹出错误。

【讨论】:

    猜你喜欢
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 2014-10-13
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多