简体   繁体   English

在Libgdx Stage中获取Image(或actor)的屏幕位置

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

I have this piece of code: 我有这段代码:

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

How to get myImage position on a screen? 如何在屏幕上获取myImage位置? I tried myImage.getX() and myImage.getImageX() both always return 0.0. 我尝试了myImage.getX()myImage.getImageX()都返回0.0。 What's wrong? 怎么了?

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. 我相信Actor上的getX()getY()是相对于它们的父容器的,所以你需要将坐标转换为“stage”坐标,然后从那里转换为“screen”坐标。 (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);

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) - 移动图像并检查位置但是getX()返回0.0可能是因为它移动并产生移动效果的相机,图像可能根本没有被移动并且在0.0的初始位置被发现(我想这可能是你缺少的东西)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM