繁体   English   中英

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

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

我有这段代码:

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

如何在屏幕上获取myImage位置? 我尝试了myImage.getX()myImage.getImageX()都返回0.0。 怎么了?

我相信Actor上的getX()getY()是相对于它们的父容器的,所以你需要将坐标转换为“stage”坐标,然后从那里转换为“screen”坐标。 (我认为有一种更简单的方法可以做到这一点,因此可能有更好的答案)。

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);

只是一个猜测!!! - 移动图像并检查位置但是getX()返回0.0可能是因为它移动并产生移动效果的相机,图像可能根本没有被移动并且在0.0的初始位置被发现(我想这可能是你缺少的东西)

暂无
暂无

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

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