我有一个名为tile的Actor,我想将其添加到一个表格中以制作类似棋盘格的东西。我可以让瓷砖绘制,但batch.draw总是要求一个位置。如果我设置位置,则不会像我想要的那样在行的末尾绘制图块。例如,
batch.draw(texture, 0, 0);
会画:
正如你所看到的,我有一个带按钮的桌子,它们正确排列在行上,因为它们不是按位置绘制的。有没有办法让我的瓷砖以同样的方式绘制?
这是我的代码:
Tile.java
public class Tile extends Actor {
boolean filled;
int row;
int column;
Texture texture;
public Tile(int row, int column){
this.row = row;
this.column = column;
texture = new Texture(Gdx.files.internal("assets/game/tile.png"));
this.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("down");
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("up");
}
});
}
@Override
public void draw (SpriteBatch batch, float parentAlpha) {
//batch.setColor(0.5f, 0.5f, 0.5f, 1);
batch.draw(texture, 0, 0);
}
public int getRow(){
return row;
}
public int getColumn(){
return column;
}
}
MainScreen.java
public class MainScreen implements Screen {
MyGame myGame;
private final Stage stage;
private final SpriteBatch spriteBatch;
Table table;
private TextureAtlas buttonsUi;
Boolean nextScreen= false;
public MainScreen(MyGame myGame) {
spriteBatch = new SpriteBatch();
stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false, spriteBatch);
this.myGame = myGame;
Gdx.input.setInputProcessor(stage);
//Create the text label
Skin pennySkin = new Skin(Gdx.files.internal("uiskin.json"));
Label pennyLabel = new Label("PennyPop", pennySkin);
//Create the sfx and api button button
ImageButton sfxButton = createSfxButton();
ImageButton apiButton = createApiButton();
ImageButton gameButton = createGameButton();
table = new Table();
table.add(sfxButton);
table.add(apiButton).padLeft(10);
table.add(gameButton).padLeft(10);
table.add(new Tile(0,0)).padLeft(10); //Tile added here to the row
table.debug();
//VericalGroup the buttons and the label
VerticalGroup vGroup = new VerticalGroup();
vGroup.addActor(pennyLabel);
vGroup.addActor(table);
//Put in another group to align correctly with api widget
Table rootTable = new Table();
rootTable.setName("root table");
rootTable.setFillParent(true);
rootTable.add(vGroup);
rootTable.debug();
// stage.addActor(pennyLabel);
stage.addActor(rootTable);
}
@Override
public void dispose() {
spriteBatch.dispose();
stage.dispose();
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
Table.drawDebug(stage);
if(nextScreen){
myGame.setScreen(new GameScreen(myGame));
dispose();
}
}
@Override
public void resize(int width, int height) {
stage.setViewport(width, height, false);
}
@Override
public void hide() {
Gdx.input.setInputProcessor(null);
}
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
@Override
public void pause() {
// Irrelevant on desktop, ignore this
}
@Override
public void resume() {
// Irrelevant on desktop, ignore this
}
答案 0 :(得分:3)
你的Tile Actor必须设置自己的大小,以便Table知道如何放置它。由于它是Actor的子类,因此它已经有可供您填写的大小字段(不要创建自己的height
和width
变量,这会隐藏正确的变量。)
texture = new Texture(Gdx.files.internal("assets/game/tile.png"));
width = texture.getWidth();
height = texture.getHeight();
然后绘制它,使用x
和y
字段(也是Actor超类的一部分)。表格将为您设置x和y值,因此您只需要这样做。
@Override
public void draw (SpriteBatch batch, float parentAlpha) {
batch.setColor(1, 1, 1, parentAlpha);
batch.draw(texture, x, y);
}
请注意,您必须始终设置颜色,即使它是白色的,因为它总是可能将批次的颜色保留为刚刚绘制的其他东西的非白色。