[英]How to extend Actor and draw it on stage? Libgdx
我正在尝试制作一个扩展Actor类的对象。 但是只要我调用了Stage的draw()方法,它只会绘制我添加到其中的按钮。 这是我的代码,也许有人可以帮助我?
屏幕类的代码(假设我已经重写了所有必需的方法,并且导入了所有必需的类):公共类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);
}
您覆盖了错误的draw
方法。 演员的绘制方法签名为:
public void draw (Batch batch, float parentAlpha)
您覆盖了Drawable中的那个。 不知道为什么要实现Drawable。 无论如何,已经有一个Actor可用于绘制文本,称为Label。
另外,如果要将Actor放在表中,则需要设置其宽度和高度,或在表中设置其单元格的宽度和高度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.