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:
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:
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.
Found the solution with the help of Tanmay and NateS from badlogic's forum, they guided me to see further about TableLayout.
What happens is that the original size of the children, in this case, doesn't affect the "board" layout. What you need to do is to use TableLayout to set the SIZE (not the prefSize) of the children, so the working code goes like this:
board.add(new Tile()).size(128, 128);
Just that, some simple misunderstanding on how to use Tables.
Thank you all.