如何在没有scene2d的情况下检测是否触摸了纹理libgdx [英] How to detect if a texture has been touched libgdx WITHOUT scene2d

查看:21
本文介绍了如何在没有scene2d的情况下检测是否触摸了纹理libgdx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在不使用scene2d 的情况下检测对某个纹理的触摸.Gdx.input.isTouched() 检测到整个屏幕上的触摸,有没有一种方法可以在没有scene2d 的情况下使用检测纹理上的触摸?

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 i could use something that detects a touch on a texture without scene2d?

推荐答案

当然有,我也没用screne2d.

Of course there is, I also never use 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
     }
}

这篇关于如何在没有scene2d的情况下检测是否触摸了纹理libgdx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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