Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LIBGDX- getting position of an actor inside a table

Tags:

libgdx

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.

enter image description here

like image 687
centenond Avatar asked Mar 13 '23 22:03

centenond


1 Answers

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

like image 143
Yasen Bagalev Avatar answered Apr 26 '23 23:04

Yasen Bagalev