简体   繁体   中英

How to extend Actor and draw it on stage? Libgdx

I am trying to make an object that extends the Actor class. but whenever I have call the Stage's draw() method, it only draws the button I added to it. 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{

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):

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. Actor's draw method signature is:

public void draw (Batch batch, float parentAlpha)

You overrode the one from Drawable. Not sure why you're implementing Drawable. There's already an Actor available for drawing text anyway, called 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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