Search code examples
javabuttonlibgdxactorstage

How stage work in Libgdx?


I'm new to stage class and actors, I know that a button is an actor so I created one:

public void create(){
    buttonAtlas=new TextureAtlas(Gdx.files.internal("buttons/button.pack"));
    skin=new Skin();
    skin.addRegions(buttonAtlas);
    buttonStyle=new Button.ButtonStyle();
    buttonStyle.up=skin.getDrawable("animation00");
    buttonStyle.down=skin.getDrawable("animation01");

    button=new Button(buttonStyle);
    button.setPosition(200,700);

    stage=new Stage();
    stage.addActor(button); //the problem is that nothing happens.
}

It would be really helpful if you can explain what is a stage and actors.


Solution

  • In LibGDX an Actor is a representation of a 2D Node Graph Object.

    The actor will have a list of actions, and contains parameters such as position, rectangular size, origin, scale, rotation, Z index, and color.

    You're Actors are basically the fundamental objects you're going to be applying Actions to thorough-out the progress of the scene on the Stage.


    Meanwhile a Stage can be thought of just like the real world stage. On the Stage you will have Actors. The Stage can also handle input events and is comparable to hierarchical view of all active Actors and their current in-progress Actions.

    A good place to read more would be the nightly Javadocs. They can be found here.

    The specific JavaDocs do a really good job of describing the Actors and Stage.