How to detect if a texture has been touched libgdx WITHOUT scene2d

前端 未结 1 356
梦如初夏
梦如初夏 2021-01-13 12:27

I want to know if there is a away to detect a touch on a certain texture without using scene2d. The Gdx.input.isTouched() detects a touch on the WHOLE screen, is there a way

相关标签:
1条回答
  • 2021-01-13 12:58

    Of course there is, I also never use screne2d.

    in your update method

      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
         }
    }
    
    0 讨论(0)
提交回复
热议问题