Question

How do I 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 that is above is the actor that has been touched. Once this particular actor has been touched I need to change the color of another actor. How exactly do I go about getting that actor by its name?

Was it helpful?

Solution 2

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

myactor.setName("myactor");

Then you can get all the Actors in the Stage this is in, like this: (Stage#getActors)

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

Then, you can use Actor#getName to check all the Actors for that name:

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;
    }
}

But it would be easier and more performant if you kept a reference to all your Actors and use it instead.

OTHER TIPS

I would like to point out that there is already a method which finds an Actor by name.

It works like this: stage.getRoot().findActor(name).

No need to implement it yourself. :)

I would recoment to use the already given functionality of the Group. Every Stage has an root Group and this implements the lookup for an actor by name. The code for it is more secure than the given inside of the answer in case you use the Grouping system.

The code of the Group looks like this and is more secure because it also looks inside of a group if you add a group to the stage.

    /** 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;
    }

Link to Group Class

If you need to search alot of times to hold an refrence of the Actor. If not just use the serch method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top