Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

LibGDX: creating actor conformed by other actors

$
0
0

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?


Viewing all articles
Browse latest Browse all 434

Trending Articles