简体   繁体   English

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

[英]How to extend Actor and draw it on stage? Libgdx

I am trying to make an object that extends the Actor class. 我正在尝试制作一个扩展Actor类的对象。 but whenever I have call the Stage's draw() method, it only draws the button I added to it. 但是只要我调用了Stage的draw()方法,它只会绘制我添加到其中的按钮。 Here is my code, maybe someone here can help me? 这是我的代码,也许有人可以帮助我?

Code for my Screen class (Assume that I had overridden all necessary methods and that I imported all necessary classes): public class PlayScreen implements 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();
}

And here is My Textdisplay class that extends Actor (Again assume all other methods have been overriden, and all necessary classes have been imported): 这是扩展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);
}

You overrode the wrong draw method. 您覆盖了错误的draw方法。 Actor's draw method signature is: 演员的绘制方法签名为:

public void draw (Batch batch, float parentAlpha)

You overrode the one from Drawable. 您覆盖了Drawable中的那个。 Not sure why you're implementing Drawable. 不知道为什么要实现Drawable。 There's already an Actor available for drawing text anyway, called Label. 无论如何,已经有一个Actor可用于绘制文本,称为Label。

Also, if you want to put your Actor in a table, you need to set its width and height, or the width and height of its cell in the table. 另外,如果要将Actor放在表中,则需要设置其宽度和高度,或在表中设置其单元格的宽度和高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM