Search code examples
javalibgdxscene2d

LibGDX - How does the parent of an Actor class work?


Beginner here so this question might be a little dumb, my apologies.

I'm trying to make a menu for a game i'm working on, and i'm using Scene2d. I know how parent and child classes work, but i got stuck trying to understand the Actor class.

I created a class called "Actor1" which extends from Actor, overrides the draw method and simply draws the default libgdx image:

public class Actor1 extends Actor{

    Texture tex = new Texture("badlogic.jpg");

    public void draw(Batch batch, float alphaParent){
        batch.draw(tex, 0, 0);
    }   
}

It works fine, but i was a little confused about the "alphaParent" parameter. So i did some research, and i've read tons of articles and questions talking about the parent class, like for example the github wiki:

If the parentAlpha is combined with this actor's alpha as shown, child actors will be influenced by the parent's translucency.

The Batch passed to draw is configured to draw in the parent's coordinates, so 0,0 is the bottom left corner of the parent.

Based on what i know, I guessed the Actor class is the Actor1 class' parent. But if so, what are the "parent's coordinates" or "parent's translucency"? Where are they initialized, or defined, and where can i change them? Do these parameters come from the Actor class or are these parameters defined by something else, like the stage?


Solution

  • No, when it talks about parents, it is not talking about class hierarchy. It's talking about the Group Actor instance that contains this Actor. Group is a subclass of Actor that can have Actor children. If you add an Actor directly to a Stage, its parent is the root Group, which sits at (0, 0), so your Actor's position is relative to the world.

    But you can add an Actor to a Group, and then its position is relative to its parent's position. And the alphaParent parameter is the transparency level of the parent.

    In the draw() method, you must always set the color of the Batch before you draw, because there is no guarantee of what color it is currently set at. And you should apply the Actor's parent alpha if you want the children of a Group to properly fade in and out along with the parent.

    A typical draw method looks like this:

        public void draw(Batch batch, float alphaParent){
            Color color = getColor();
            batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
            batch.draw(tex, 0, 0);
        }