如何匹配用户单击和libGDX框架中的sprite对象位置 [英] How to match user click and the sprite object position in libGDX framework

查看:120
本文介绍了如何匹配用户单击和libGDX框架中的sprite对象位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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返回相对于正凸轮宽度和高度的co-ords,它是250x120像素和click co-ords是相对于设备的宽度和高度,可能是根据设备的任何东西。因此,即使我点击对象上的点击标记和对象位置标记在它们的值上有巨大的差异。所以我永远不会得到匹配的值。

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框架中的sprite对象位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆