gpt4 book ai didi

java - 如何扩展Actor并将其绘制到舞台上? Libgdx

转载 作者:行者123 更新时间:2023-12-02 03:26:45 26 4
gpt4 key购买 nike

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

我的 Screen 类的代码(假设我已经重写了所有必要的方法并且导入了所有必要的类):公共(public)类 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方法。 Actor 的绘制方法签名是:

public void draw (Batch batch, float parentAlpha)

您覆盖了来自 Drawable 的那个。不确定为什么要实现 Drawable。无论如何,已经有一个可用于绘制文本的 Actor,称为​​“Label”。

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

关于java - 如何扩展Actor并将其绘制到舞台上? Libgdx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38753184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com