如何在 libGDX 框架中匹配用户点击和精灵对象位置

时间:2023-04-06
本文介绍了如何在 libGDX 框架中匹配用户点击和精灵对象位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 libGDX java 框架在 Eclipse 中开发一个练习游戏.

I am using libGDX java framework for developing a practice game in Eclipse.

我的游戏处于横向模式,我正在使用精灵图像作为游戏资产.实际上我正在尝试遵循千螺栓 ZombieBird 教程

My game is in landscape mode and I am using sprite image for game assets .Actually i am trying to follow the kilobolt ZombieBird tutorial

我已经设置了这样的正交相机-->

I have set orthographic camera like this -- >

   cam = new OrthographicCamera();
   cam.setToOrtho(true, 250, 120);

我这样做是因为我的背景纹理区域在精灵图像中是 250 x 120 像素.

I have done this because my background texture region is of 250 x 120 px in the sprite image.

所以基本上我的精灵图像尺寸很小,它会根据设备进行缩放,但是所有的计算都是相对于 250 x 140 像素完成的,比如改变我定义的对象的位置 Vector2 position =new Vector2(x, y); 如果我写 position.x = 260; 即使我的设备宽度为 500px,精灵也会出现在屏幕之外.

So basically my sprite image is small in size and it is getting scaled according to the device but all the computing is done relative to 250 x 140 px like for changing the position of the object i have defined Vector2 position = new Vector2(x, y); and if i write position.x = 260; the sprite will go outside the screen even if my device width is 500px .

问题:现在我必须让移动的精灵在有人点击它时消失(想象一下僵尸四处移动,如果我点击它们它们就会死掉).所以我使用以下代码来匹配用户点击坐标和对象坐标.

Problem : Now i have to make the moving sprite vanish when someone clicks on it (just imagine zombies moving around and if i click on them they die) .So i am using the following code for matching user click co-ords with the object co-ords.

 int x1 = Gdx.input.getX();
 int y1 = Gdx.input.getY();
 if(position.x == x1 && position.y == y1){
      // do something that vanish the object clicked
  }

问题是 position.x 和 position.y 返回相对于正射凸轮宽度和高度的坐标,即 250x120 像素,点击坐标是相对于设备的宽度和高度,这可能是根据设备.因此,即使我在对象上单击鼠标右键,单击坐标和对象位置坐标的值也存在巨大差异.所以我永远不会得到匹配的值.

The problem is position.x and position.y returns the co-ords relative to the ortho cam width and height which is 250x120 px and the click co-ords are relative to the device width and height which maybe anything according to the device. Because of this even if i click right on the object the click co-ords and the object position co-ords have a huge difference in their values.So i would never get matching values .

有什么解决办法还是我做错了?

Is there any solution for this or am i doing it wrong ?

推荐答案

您必须使用相机取消投影设备坐标.相机具有执行此操作的内置功能,因此非常简单.此外,要确定精灵是否被点击,您必须检查点击的点是否在精灵内部的任何位置,而不仅仅是等于精灵的位置.做这样的事情:

You have to unproject the device coordinates using the camera. The camera has a built in function to do this, so it's fairly simple. Furthermore, to determine if the sprite is clicked, you have to check to see if the point clicked is anywhere inside the sprite, not just equal to the sprite's position. Do something like this:

int x1 = Gdx.input.getX();
int y1 = Gdx.input.getY();
Vector3 input = new Vector3(x1, y1, 0);
cam.unproject(input);
//Now you can use input.x and input.y, as opposed to x1 and y1, to determine if the moving
//sprite has been clicked
if(sprite.getBoundingRectange().contains(input.x, input.y)) {
    //Do whatever you want to do with the sprite when clicked
}

这篇关于如何在 libGDX 框架中匹配用户点击和精灵对象位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:libGDX:如何实现基于平铺/网格的游戏角色移动? 下一篇:如何在非 GUI 服务器环境中运行 libGDX 应用程序?

相关文章