2

Oh well... after almost one day trying to figure it out, couldn't find it so... here it is. I'm using scene2d.

My problem is this one, I have a class named "Tile" that extends the Image class (that extends widget) and has a texture in it of size 128x128, but when I try to add it into a table (Widget Group), my Tile disappears.

Edit: I just made a size check on the table and verified that after adding a Tile, which is 128x128 in size, inside the table, the table is showing itself with a size of 0x0... something wrong here maybe?

I already tried:

  • Using addActor() instead of add(), and the Tile shows perfectly, but can't use the table the way i need...
  • Adding other actors with add(), and they show up.
  • Adding the Tile in the middle of other actors, and what happens is that the Tile doesn't appear at all, not even as an empty space (that would happen if you add a null object to a table).
  • Seeing if there is something wrong with the Preferred size of the Tile, which is all right and happily being 128x128.

OBS: One thing that i strangely observed is that when i try to use setDrawable() in my Tile constructor (making the appropriate conversions of Texture->TextureRegion->TextureRegionDrawable), i can't have it drawn by any means, i just can make it get my texture by calling super(Texture) at the constructor, and observing the Image(Drawable) constructor, i happen to notice it does some other things when setting the drawable for the first time, BUT JUST FOR THE FIRST TIME... AND... even using super(Texture), it doesn't show when adding to a cell into a table... Maybe the problem is here? I don't know...

My questions are:

  • What is the problem?
  • If understanding the problem isn't enough to solve it, what can i do?

Edit2: As requested, here is the relevant code:

public class Tile extends Image{

// Tiles
private Texture unselected;
private Texture selected;

// InputListener for detecting mouse over
private class MouseOver extends InputListener{

    public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
        setDrawable(new TextureRegionDrawable(new TextureRegion(selected)));
    }
}
private class MouseOut extends InputListener{

    public void exit(InputEvent event, float x, float y, int pointer, Actor fromActor) {
        setDrawable(new TextureRegionDrawable(new TextureRegion(unselected)));
    }
}

public Tile(){
    super(TextureFactory.unselectedTile());
    unselected = TextureFactory.unselectedTile();
    selected = TextureFactory.selectedTile();
    this.addListener(new MouseOver());
    this.addListener(new MouseOut());
}
}

The Tile has a listener that when i mouse it over it changes its texture. I removed those listeners for testing and the error was still occurring, so... not them.

The Screen that implements the Tile (Some things are commented because i was expecting to use it like that...)

public class CombatScreen extends AbstractScreen{

private Table board;

public CombatScreen(SpaceGameMain game, int boardSize) {
    // Passing game to super class and getting class name for tracking
    super(game);
    this.className = this.getClass().getSimpleName();

    // Creating tile board
    board = new Table();
    /*
    for(int i = 0; i < boardSize; i++)
    {
        for(int j = 0; j < boardSize; j++){
            board.add(new Tile());
        }
        board.row();
    }
    */
}

@Override
public void show(){
    super.show();
    board.add(new Tile());
    mainStage.addActor(board);
}

public void resize(int width, int height){
    super.resize(width, height);

    board.setPosition((Gdx.graphics.getWidth() - Tile.width)/2, (Gdx.graphics.getHeight() - Tile.height)/2);
}
}

The Table is set to the middle of the screen for testing.

4

3 回答 3

3

在 badlogic 论坛的 Tanmay 和 NateS 的帮助下找到了解决方案,他们引导我进一步了解 TableLayout。

在这种情况下,孩子的原始大小不会影响“板”布局。您需要做的是使用 TableLayout 来设置孩子的 SIZE(而不是 prefSize),所以工作代码是这样的:

board.add(new Tile()).size(128, 128);

就是这样,关于如何使用表格的一些简单误解。

谢谢你们。

于 2013-12-16T01:05:00.913 回答
1

Table 类在使用 add 方法时,在布局子项时调用 getPrefWidth() 和 getPrefHeight()。

它们的默认值为 0。

解决方案 1)在 Tile 类中覆盖 getPrefWidth() 和 getPrefHeight() 并让它们返回你想要的任何东西。

解决方案 2)使用 prefSize 的链表添加方法。例如

table.add(actor).prefSize(200,200);
于 2013-12-13T17:49:27.163 回答
0

除此之外,如果上述答案对其他人(如我)不起作用,则调用 image.setScaling(Scaling.none) 或 image.setScaling(Scaling.fit) 也可能是解决方法。

于 2020-01-23T20:29:18.067 回答