【发布时间】: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);
}
我应该在 scaleSkinToMatchScreen() 中添加什么来将所有皮肤图片缩放到我的坐标?
【问题讨论】:
-
更新:我尝试渲染的按钮使用 NinePatch,如果我将所有填充/分割设置为 0,那么图片在框内渲染得很好,但字体仍然混乱且巨大。描述如何缩放 NinePatch 图像以与我在舞台上设置的网格相结合的像素区域似乎没有充分的理由。鉴于我的图片渲染良好(即 1 个像素!= 1 个单位),我不明白为什么按钮应该有任何不同...
标签: java libgdx coordinates scene2d