我正在使用libgdx制作一个简单的游戏,我想使用scene2Dui和舞台和皮肤来简化我的定位/大小调整代码 .
为了进一步简化所有内容的定位,我还想定义一个自定义坐标系,这样无论屏幕大小如何,代码都会自动创建一个网格覆盖,并正确地定位/调整所有元素 .
我遇到的问题与这个新的坐标系有关,以及它如何与皮肤一起工作 . 基本上,我根据皮肤中的样式创建一个按钮,并将其放在一个表格中,根据我的坐标进行调整 . 我也创建了一个图像,但这次直接加载区域并自己定位 .
调试行显示按钮本身工作正常,它的行为与点击的预期相同;如果我在 table 外面点击,尽管图像被炸毁,但没有反应 . 调整窗口大小也符合预期 .
在某个地方,舞台必须要求皮肤按钮的图片有多大,并且它以像素回答它应该在我的坐标中回答 . 有没有办法将皮肤缩放到我的坐标,并在调整窗口大小时更新它?
这是相关的代码:
// *** 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()中放置什么来将所有皮肤的图片缩放到我的坐标?
1 回答
是的按钮使用了九个补丁 . 您可以通过在JSON中专门定义样式(包括字体)并引用纹理图集的包文件来进行设置 . scene2d ui库在最好的时候是挑剔的,所以它需要一些游戏来让事情正常工作 . 我还建议使用热代码交换来获得正确的定位 . 它比为每次更改重新启动应用程序要快得多 .
热代码交换(假设您正在使用eclipse,不能代表其他IDE):
以调试模式运行程序 .
在IDE代码编辑器中进行更改
保存更改
更改已交换并应显示在您的应用程序中 .
您也可以使用此工具:
https://github.com/cobolfoo/gdx-skineditor
就它的开发而言,它还处于早期阶段,但是一旦你解决了这些错误和怪异,它确实能很好地工作 .
毫无疑问,您的libGDX应该更新到最新版本 . 它是开源的,并且会不时弹出错误 .