在过去的几天里,我一直在将我的游戏 ( Apopalypse ) 移植到 Android 移动平台。我在 Google 上快速搜索了 Sprite 触摸检测,但没有找到任何有用的信息。每个气球一旦被触摸就会弹出,我只需要检测它是否被触摸。 这是我的气球生成代码:
渲染(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);
}
最佳答案
在您的类中使用 render 方法可以执行以下代码:
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
}
}
}
关于java - 我如何检测是否在 Java libGDX 中触摸了 Sprite ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24501268/