Get actor by name in libgdx

How to get an actor by name in libgdx?

I currently have the following ChangeListener:

    ChangeListener colorPickerListener = new ChangeListener()
    {
        public void changed(ChangeEvent event, Actor actor)
        {
            //Popup Window
            toolboxStage.addActor(blockWindow);
            //toolboxStage.getRoot().removeActor(blockWindow);
            Gdx.app.log("LevelEditorScreen", "Color Picker Selected");
            Gdx.app.log("LevelEditorScreen", "HUD Width: " + HUD_WIDTH);

            Gdx.input.setInputProcessor(toolboxStage);
        }
    };

The actor who is above is the actor who has been affected. Once this particular actor has been touched, I need to change the color of another actor. How exactly am I going to get this actor by his name?

+4
source share
3 answers

First you need to specify a name for your Actor: ( Actor # setName )

myactor.setName("myactor");

Then you can get all the actors on the stage in which it is located, for example: ( Stage # getActors )

Array<Actor> stageActors = mystage.getActors();

Actor # getName, :

int len = stageActors.size;
for(i=0; i<len; i++){
    Actor a = stageActors.get(i);
    if(a.getName().equals("myactor")){
        //a is your Actor!
        break;
    }
}

, .

+3

, , Actor .

: stage.getRoot().findActor(name).

.:)

+15

. , . , , .

, , .

    /** Returns the first actor found with the specified name. Note this recursively compares the name of every actor in the group. */
    public Actor findActor (String name) {
            Array<Actor> children = this.children;
            for (int i = 0, n = children.size; i < n; i++)
                    if (name.equals(children.get(i).getName())) return children.get(i);
            for (int i = 0, n = children.size; i < n; i++) {
                    Actor child = children.get(i);
                    if (child instanceof Group) {
                            Actor actor = ((Group)child).findActor(name);
                            if (actor != null) return actor;
                    }
            }
            return null;
    }

If you need to search many times to keep the review of the Actor. If you do not just use the "PICTURE" method.

+1
source

All Articles