如何扩展Actor并在舞台上绘制它? Libgdx

时间:2016-08-03 20:38:01

标签: java libgdx

我正在尝试创建一个扩展Actor类的对象。但每当我调用Stage的draw()方法时,它只会绘制我添加到它的按钮。这是我的代码,也许这里有人可以帮助我?

我的Screen类的代码(假设我已经覆盖了所有必要的方法并且我导入了所有必需的类): 公共类PlayScreen实现Screen {

SpriteBatch batch;
Game game;
Table table;
Skin skin;
Stage stage;
BitmapFont font;
TextButton button;
TextDisplay display;


public PlayScreen(MyGdxGame game, SpriteBatch batch){

    this.game = game;
    this.batch = batch;

    //instantiating Stage, Table, BitmapFont, and Skin objects.
    font = new BitmapFont(Gdx.files.internal("MyTruefont.fnt"));
    table = new Table();
    stage = new Stage();
    skin = new Skin(Gdx.files.internal("Test.json"));


    button = new TextButton("Start!", skin, "default");
    display = new TextDisplay(font, this);

    //Setting table properties
    table.setWidth(stage.getWidth());
    table.align(Align.center|Align.top);
    table.setPosition(0, Gdx.graphics.getHeight());
    table.padTop(30);

    //Adding actors to table
    table.add(button).padBottom(50);
    table.add(display);

    //Adding table to stage and setting stage as the input processor
    stage.addActor(table);
    Gdx.input.setInputProcessor(stage);
}



@Override
public void show() {

}

@Override
public void render(float delta) {

    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    stage.draw();
    batch.end();
}

这是扩展Actor的My Textdisplay类(再次假设所有其他方法都已被覆盖,并且已导入所有必需的类):

public class TextDisplay extends Actor implements Drawable {

    Texture texture;
    BitmapFont font;
    String str;

    public TextDisplay(BitmapFont font, Screen screen){
        this.font = font;

        texture = new Texture(Gdx.files.internal("TextArea.png"));

        str = "";

        setLeftWidth(texture.getWidth());
        setRightWidth(texture.getWidth());

        setBottomHeight(texture.getHeight());
        setTopHeight(texture.getHeight());
    }



@Override
public void draw(Batch batch, float x, float y, float width, float height) {
    batch.draw(texture, x, y);
}

1 个答案:

答案 0 :(得分:2)

您覆盖了错误的draw方法。 Actor的draw方法签名是:

public void draw (Batch batch, float parentAlpha)

你覆盖了Drawable中的那个。不确定为什么要实施Drawable。无论如何,已经有一个Actor可用于绘制文本,称为Label。

此外,如果您想将Actor放在表格中,您需要在表格中设置其宽度和高度,或其单元格的宽度和高度。

相关问题