0
\$\begingroup\$

I am creating a custom class that extends from Actor,just like this:

public class MyActor extends Actor {
ProgressBar bar;
Label nameLabel;
TextButton button;
public MyActor(String name, Skin skin){
    nameLabel = new Label(name, skin);
    button = new TextButton("Whatever",skin);
    bar = new ProgressBar(0f,5f,1f,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);
} }

Then I try to add it to a Table:

Table myTable = new Table();
myTable.add(myActor).row();

But it always gets drawn in the 0,0 coordinates!, what am I doing wrong? Do I have to override another method?

\$\endgroup\$
4
  • \$\begingroup\$ Add the code for myTable instead of MyActor. \$\endgroup\$
    – ott--
    Feb 3, 2016 at 21:45
  • \$\begingroup\$ @ott-- How????? \$\endgroup\$ Feb 3, 2016 at 22:03
  • \$\begingroup\$ Edit your question. \$\endgroup\$
    – ott--
    Feb 3, 2016 at 22:07
  • \$\begingroup\$ Did you ever find a solution to this? \$\endgroup\$ Jan 31, 2017 at 15:15

2 Answers 2

1
\$\begingroup\$

I know its late but it might help someone, basically when we create actors, in the act() we'll have to set its layout boundary relative to the parent. Layout boundary helps identify the size of the custom actor.

someActor.setBounds( getParent().getX()+getX(), getParent.getY()+getY(), someActor.getPrefWidth(), someActor.getPrefHeight() );
\$\endgroup\$
0
\$\begingroup\$

I ran into a similar problem where despite multiple instances of my actor in table.add(myActor) they just stacked on top of each other. That is they weren't put into separate rows/columns visually using scene2d's Table.

I updated myActor to extend Widget (instead of extending Actor) and implemented getPrefWidth and getPrefHeight which solved the problem.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .