1
\$\begingroup\$

I am making my custom actor class, made up by 3 actors(oneProgressBar, one Label and one TextButton), because I will be using this many times, so it's not a good idea to create 3 of that actors when I can create one that can contain them. So I am making my class:

public class MyActor extends Actor {
ProgressBar bar;
Label nameLabel;
TextButton button;
public MyActor(String name, Skin skin, float progress){
    nameLabel = new Label(name, skin);
    button = new TextButton("Buy", skin);
    bar = new ProgressBar(0f,5f,progress,false,skin);
}


@Override
public void act(float delta) {
    super.act(delta);
    bar.setBounds(getX(), getTop()-getHeight() / 3f, getWidth(), getHeight() / 3f);
    nameLabel.setBounds(getX(), getY()+getHeight() / 3f, getWidth(), getHeight() / 3f);
    button.setBounds(getX(), getY(), getWidth(), getHeight() / 3f);
}

@Override
public void draw(Batch batch, float parentAlpha) {
    super.draw(batch, parentAlpha);
    bar.draw(batch, parentAlpha);
    nameLabel.draw(batch, parentAlpha);
    button.draw(batch,parentAlpha);
}


}

So in my game, I add my actor to the Stage using a table:

Table table = new Table();
table.debug();
MyActor actor = new MyActor("whatever",mySkin,1f);
table.add(actor).size(myWidth,myHeight);

The actor is drawn in the correct place(I know because of the debug lines), but the 3 actor are drawn in the 0,0 coordinates, what am I doing wrong? do I need to override another method?

\$\endgroup\$
1
\$\begingroup\$

It looks like your problem could be that your MyActor class does not report it's width and height to the parent Actor. Just because you have an Actor that has some other Actors as members doesn't mean the "parent" Actor will know how tall and wide it should be to accommodate all of them.

By querying the preferred size of the child Actors you can return more sensible values by overriding getHeight and getWidth;

public class MyActor extends Actor {
    Label nameLabel;
    TextButton button;
    ProgressBar bar;

    public MyActor(String name, Skin skin, float progress){
        nameLabel = new Label(name, skin);
        button = new TextButton("Buy", skin);
        bar = new ProgressBar(0f, 5f, progress, false, skin);
    }

    @Override
    public void act(float delta) {
        super.act(delta);

        button.setBounds(getX(), getY(), getWidth(), button.getPrefHeight());
        nameLabel.setBounds(getX(), button.getTop(), getWidth(), nameLabel.getPrefHeight());
        bar.setBounds(getX(), nameLabel.getTop(), getWidth(), nameLabel.getPrefHeight());
    }

    @Override
    public float getWidth() {
        return Math.max(bar.getPrefWidth(), Math.max(nameLabel.getPrefWidth(), button.getPrefWidth()));
    }

    @Override
    public float getHeight() {
        return nameLabel.getPrefHeight() + button.getPrefHeight() + bar.getPrefHeight();
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        nameLabel.draw(batch, parentAlpha);
        bar.draw(batch, parentAlpha);
        button.draw(batch,parentAlpha);
    }
}

I also changed the layout logic to consider the preferred size of the children, instead of saying that each should be a third of the whole in height, for example.

Also, if your table is the root table, I think you want to set it to fill the parent;

Table table = new Table();
table.setFillParent(true);
\$\endgroup\$
  • \$\begingroup\$ It still gets drawn in the left bottom corner of the screen, it's just longer. :/ \$\endgroup\$ – Asaltaviejas Feb 12 '16 at 0:25
  • \$\begingroup\$ @Asaltaviejas Is your table the root table? If this is the case is should probably fill its parent. I've added a part to the end of my answer about this. \$\endgroup\$ – bornander Feb 12 '16 at 7:20
  • 1
    \$\begingroup\$ I already solved it, instead of doing MyActor extends from Actor it extends from Group and I just added the 2 actor to the it. :D \$\endgroup\$ – Asaltaviejas Feb 12 '16 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.