如何检测纹理是否已被触摸libgdx没有scene2d

时间:2014-07-18 21:58:09

标签: libgdx

我想知道在不使用scene2d的情况下是否存在检测某个纹理的触摸的距离。 Gdx.input.isTouched()检测到整个屏幕上的触摸,有没有办法可以使用一些东西来检测没有scene2d的纹理上的触摸?

1 个答案:

答案 0 :(得分:12)

当然有,我也从不使用screne2d。

在您的更新方法

  if(Gdx.input.isTouched())
{
  Vector3 tmp=new Vector3(Gdx.input.getX(),Gdx.input.getY();
  camera.unproject(tmp);
  Rectangle textureBounds=new Rectangle(textureX,textureY,textureWidth,textureHeight);
  // texture x is the x position of the texture
  // texture y is the y position of the texture
  // texturewidth is the width of the texture (you can get it with texture.getWidth() or textureRegion.getRegionWidth() if you have a texture region
   // textureheight is the height of the texture (you can get it with texture.getHeight() or textureRegion.getRegionhHeight() if you have a texture region
  if(textureBounds.contains(tmp.x,tmp.y))
     {
     // you are touching your texture
     }
}