[英]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.