Well the title says it all, I want to get the position of an actor inside a table
Button button = new Button(bs);
table.add(button).width(50).height(50);
table.top().center();
Thanks in advance.
edit: with button.localToStageCoordinates(new Vector2(button.getX(), button.getY()));
i get a near position but not exact, here is an screen shot, red squares are the positions im getting, while the transparent squares are the buttons.
Try:
button.localToStageCoordinates(new Vector2(button.getX(), button.getY()));
This should return a Vector2 with the real position. I think that the table.validate() should be called before you can get the position of your button.
validate() calls layout(). layout() will be automatically called on the next draw after you create the table. If you need the position right after the initialization of the table - I think that you will need to call the layout() method.
layout() description:
Computes and caches any information needed for drawing and, if this actor has children, positions and sizes each child, calls invalidate() any each child whose width or height has changed, and calls validate() on each child. This method should almost never be called directly, instead validate() should be used. Overrides: layout() in WidgetGroup
validate() description:
Ensures the actor has been laid out. Calls layout() if invalidate() has been called since the last time validate() was called, or if the actor otherwise needs to be laid out. This method is usually called in Actor.draw(Batch, float) before drawing is performed. Specified by: validate() in Layout
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With