Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get screen position of Image (or actor) in Libgdx Stage

Tags:

java

libgdx

I have this piece of code:

Image myImage = new Image(new Texture("data/file.png"));

How to get myImage position on a screen? I tried myImage.getX() and myImage.getImageX() both always return 0.0. What's wrong?

like image 748
Alf Avatar asked Oct 19 '25 15:10

Alf


2 Answers

I believe getX() and getY() on an Actor are relative to their parent container, so you'll need to convert the coordinates to "stage" coordinates, and then from there to "screen" coordinates. (I think there is an easier way to do this, so there may be a better answer out there).

Image myImage = ...;
Vector2 coords = new Vector2(myImage.getX(), myImage.getY());
myImage.localToStageCoordinates(/*in/out*/coords);
myImage.getStage().stageToScreenCoordinates(/*in/out*/coords);

System.out.println("Image X " +myImage.get()+ " maps to screen " +coords.x);
like image 187
P.T. Avatar answered Oct 22 '25 05:10

P.T.


Just a guess!!! - Moving the image & checking for position but getX() returning 0.0 might be because its the camera which moves & produces the movement effect, image may havenot been moved at all & tsayed at the initial position of 0.0 (i think this might be the thing you are missing)