当前位置: 首页 > 知识库问答 >
问题:

如何检测Java libGDX中是否触摸了精灵?

庞安晏
2023-03-14

在过去的几天里,我一直在将我的游戏(Apopalypse)移植到Android Mobile平台。我在Google上快速搜索了精灵触摸检测,但没有找到任何有用的东西。每个气球触摸一次就会弹出,我只需要检测它是否被触摸。这是我的气球生成代码:

渲染(x、y、宽度和高度随机):

public void render() {
    y += 2;
    balloon.setX(x);
    balloon.setY(y);
    balloon.setSize(width, height);
    batch.begin();
    balloon.draw(batch);
    batch.end();
}

在主游戏类中产卵:

addBalloon(new Balloon());

public static void addBalloon(Balloon b) {
    balloons.add(b);
}

共有3个答案

农英杰
2023-03-14

我做了一个小类,用于我的游戏检测雪碧是否被触摸

public class SpriteTouchable extends Sprite {

    public SpriteTouchable(Texture texture) {
        super(texture);
    }

    public SpriteTouchable(Sprite sprite) {
        set(sprite);
    }

    public static float xTrans(float x)
    {
        return x*Gdx.graphics.getWidth()/480;
    }

    public static float yTrans(float y)
    {
        return y*Gdx.graphics.getHeight()/320;
    }

    private boolean isTouched;

    /**
     * Type: Input Listener function
     * listen if this sprite button was pressed (touched)
     * @param marge : the extra touchable space out of sprite
     * @param x     : x position touched by user
     * @param y     : y position touched by user
     * 
     * return true  : Sprite touched
     * return false : Sprite not touched
     */
    public boolean isPressing(int marge,int x, int y) {
        if((x>getX() -xTrans(marge))&& x<getX() +getWidth()+xTrans(marge)) {
            if((y>getY() -yTrans(marge))&& y<getY()+getHeight()+yTrans(marge)) {
                return true;
            }
        }
        return false;
    }
}

 public boolean isTouched() {
    return isTouched;
 }

这里是我如何使用它

Gdx.input.setInputProcessor(new GameInputListener() {

            @Override
            public boolean touchUp(int screenX, int screenY, int pointer, int button) {

                return false;
            }

            @Override
            public boolean touchDown(int x, int yy, int pointer, int button) {
                int y = Gdx.graphics.getHeight() - yy;
                // if sprite + 10 of px marge is touched
                if(mySpriteTouchable.isPressing(10, x, y)) {
                    // sprite is touched down
                }
                return false;
            }
}

使用相同的逻辑,您可以检测精灵释放,也可以自定义精灵被触摸时的效果大小等等…

希望这是有帮助的!

郏志学
2023-03-14

我是这样做的,但是根据你使用的场景和可以触摸的元素,可以有稍微更优化的方法来做到这一点:

public GameScreen implements Screen, InputProcessor
{

  @Override
  public void show()
  {
      Gdx.input.setInputProcessor(this);
  }

  @Override
  public boolean touchDown(int screenX, int screenY, int pointer, int button)
  {
      float pointerX = InputTransform.getCursorToModelX(windowWidth, screenX);
      float pointerY = InputTransform.getCursorToModelY(windowHeight, screenY);
      for(int i = 0; i < balloons.size(); i++)
      {
          if(balloons.get(i).contains(pointerX, pointerY))
          {
              balloons.get(i).setSelected(true);
          }
      }
      return true;
   }

   @Override
   public boolean touchUp(int screenX, int screenY, int pointer, int button)
   {
       float pointerX = InputTransform.getCursorToModelX(windowWidth, screenX);
       float pointerY = InputTransform.getCursorToModelY(windowHeight, screenY);
       for(int i = 0; i < balloons.size(); i++)
       {
           if(balloons.get(i).contains(pointerX, pointerY) && balloons.get(i).getSelected())
           {
               balloons.get(i).execute();
           }
           balloons.get(i).setSelected(false);
       }
       return true;
    }

public class InputTransform
{
    private static int appWidth = 480;
    private static int appHeight = 320;

    public static float getCursorToModelX(int screenX, int cursorX) 
    {
        return (((float)cursorX) * appWidth) / ((float)screenX); 
    }

    public static float getCursorToModelY(int screenY, int cursorY) 
    {
        return ((float)(screenY - cursorY)) * appHeight / ((float)screenY) ; 
    }
}
巫马炫明
2023-03-14

在具有render方法的类中,use可以执行以下代码:

Vector3 touchPoint=new Vector3();

void update()
{
  if(Gdx.input.justTouched())
   {
    //unprojects the camera
    camera.unproject(touchPoint.set(Gdx.input.getX(),Gdx.input.getY(),0));
    if(balloon.getBoundingRectangles().contains(touchPoint.x,touchPoint.y))
     {
      // will be here when balloon will be touched
     }
    }
   }
 类似资料:
  • 问题内容: 我试图检测我的sprite节点是否已被触摸并且我不知道从哪里开始。 问题答案: 首先将的属性设置为字符串。 然后在功能上 这是一种方法。 您也可以继承并覆盖其中的内部。 然后做

  • 问题内容: 我发现了一个类似的问题,但是我试图检测并识别用户触摸哪个Sprite,但我不知道该怎么做。这是我的变量: 这个想法是先确定spriteNode,然后将其替换为其他sprite或更改颜色,但是我不知道如何使用此spriteNodes矩阵来实现它,我想第一步是确定sprite。 问题答案: 您正在尝试执行的操作(即使我没有看到这样做的原因)也可以使用委派模式来完成。基本上,您将告诉您的代表

  • 本文向大家介绍sprite-kit 检测触摸,包括了sprite-kit 检测触摸的使用技巧和注意事项,需要的朋友参考一下 示例 您可以覆盖4种SKScene检测用户触摸的方法 请注意,每种方法都接收一个touches参数(在特定情况下),该参数可以包含一个以上的单个触摸事件。

  • 我正在做一个关于Android Studio的项目。我需要在后台服务中检测屏幕是否被触摸(并弹出一条消息)。但是,在不影响用户使用智能手机的情况下,我无法在后台服务中检测屏幕是否被触摸。 当我说“后台检测”时,我的意思是应用程序的效果不会干扰用户在屏幕上所做的事情。我下面的进度应该解释一下我在这里的意思: 我做了一个应用程序,通过在Android系统中实现onTouchListener来检测用户是

  • 问题内容: 在不使用触摸屏设备的情况下,使用媒体查询最安全的方法是什么?如果无法解决,您是否建议使用JavaScript解决方案,例如Modernizr或Modernizr? 问题答案: 我建议使用modernizr并使用其媒体查询功能。 但是,使用CSS时,存在伪类,例如Firefox。您可以使用:-moz-system-metric(touch- enabled)。但是,并非所有浏览器都提供这

  • 问题内容: 今天早上有一篇帖子问有多少人禁用JavaScript。然后我开始想知道可以使用什么技术来确定用户是否禁用了它。 有谁知道一些简短/简单的方法来检测是否禁用了JavaScript?我的目的是警告您,如果没有启用JS的浏览器,站点将无法正常运行。 最终,我想将它们重定向到可以在没有JS的情况下运行的内容,但是我需要将此检测作为占位符才能启动。 问题答案: 我假设你正在尝试确定是否提供Jav