1. <legend id='Dr8B6'><style id='Dr8B6'><dir id='Dr8B6'><q id='Dr8B6'></q></dir></style></legend>

    <i id='Dr8B6'><tr id='Dr8B6'><dt id='Dr8B6'><q id='Dr8B6'><span id='Dr8B6'><b id='Dr8B6'><form id='Dr8B6'><ins id='Dr8B6'></ins><ul id='Dr8B6'></ul><sub id='Dr8B6'></sub></form><legend id='Dr8B6'></legend><bdo id='Dr8B6'><pre id='Dr8B6'><center id='Dr8B6'></center></pre></bdo></b><th id='Dr8B6'></th></span></q></dt></tr></i><div id='Dr8B6'><tfoot id='Dr8B6'></tfoot><dl id='Dr8B6'><fieldset id='Dr8B6'></fieldset></dl></div>

    <small id='Dr8B6'></small><noframes id='Dr8B6'>

      <bdo id='Dr8B6'></bdo><ul id='Dr8B6'></ul>
    <tfoot id='Dr8B6'></tfoot>

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

      时间:2023-07-28
          <tbody id='ai1aV'></tbody>
          <bdo id='ai1aV'></bdo><ul id='ai1aV'></ul>
        • <i id='ai1aV'><tr id='ai1aV'><dt id='ai1aV'><q id='ai1aV'><span id='ai1aV'><b id='ai1aV'><form id='ai1aV'><ins id='ai1aV'></ins><ul id='ai1aV'></ul><sub id='ai1aV'></sub></form><legend id='ai1aV'></legend><bdo id='ai1aV'><pre id='ai1aV'><center id='ai1aV'></center></pre></bdo></b><th id='ai1aV'></th></span></q></dt></tr></i><div id='ai1aV'><tfoot id='ai1aV'></tfoot><dl id='ai1aV'><fieldset id='ai1aV'></fieldset></dl></div>

          • <legend id='ai1aV'><style id='ai1aV'><dir id='ai1aV'><q id='ai1aV'></q></dir></style></legend>

            <small id='ai1aV'></small><noframes id='ai1aV'>

            <tfoot id='ai1aV'></tfoot>

                本文介绍了如何在 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 框架中匹配用户点击和精灵对象位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                相关文章

                最新文章

                <legend id='4XLc0'><style id='4XLc0'><dir id='4XLc0'><q id='4XLc0'></q></dir></style></legend>

                  <tfoot id='4XLc0'></tfoot>

                      <bdo id='4XLc0'></bdo><ul id='4XLc0'></ul>

                    <small id='4XLc0'></small><noframes id='4XLc0'>

                  1. <i id='4XLc0'><tr id='4XLc0'><dt id='4XLc0'><q id='4XLc0'><span id='4XLc0'><b id='4XLc0'><form id='4XLc0'><ins id='4XLc0'></ins><ul id='4XLc0'></ul><sub id='4XLc0'></sub></form><legend id='4XLc0'></legend><bdo id='4XLc0'><pre id='4XLc0'><center id='4XLc0'></center></pre></bdo></b><th id='4XLc0'></th></span></q></dt></tr></i><div id='4XLc0'><tfoot id='4XLc0'></tfoot><dl id='4XLc0'><fieldset id='4XLc0'></fieldset></dl></div>