我有两个精灵,他们需要同时触摸,所以我做了两个Vector2 xy和xy1。
//xy = x and y coordinate of pointer 1
//xy1 = x x and y coordinate of pointer 2
//faces is the class for the sprites
if(xy.x >= faces.faceSpr.getX() && xy.x <= faces.faceSpr.getX() + faces.faceSpr.getWidth() && //detecting if xy and xy1 are inside the sprites
xy.y >= faces.faceSpr.getY() && xy.y <= faces.faceSpr.getY() + faces.faceSpr.getHeight() && //*
xy1.x >= faces.faceSpr1.getX() && xy1.x <= faces.faceSpr1.getX() + faces.faceSpr1.getWidth() && //*
xy1.y >= faces.faceSpr1.getY() && xy1.y <= faces.faceSpr1.getY() + faces.faceSpr1.getHeight()) //*
score += 1;
else if(xy1.x >= faces.faceSpr.getX() && xy1.x <= faces.faceSpr.getX() + faces.faceSpr.getWidth() && //*
xy1.y >= faces.faceSpr.getY() && xy1.y <= faces.faceSpr.getY() + faces.faceSpr.getHeight() && //*
xy.x >= faces.faceSpr1.getX() && xy.x <= faces.faceSpr1.getX() + faces.faceSpr1.getWidth() && //*
xy.y >= faces.faceSpr1.getY() && xy.y <= faces.faceSpr1.getY() + faces.faceSpr1.getHeight())
我还没有测试过这段代码是否有效,是否有更简单,更好的方法呢?
编辑:
我尝试使用getBoundingRectangle()方法但它可以正常工作,但我在设置它的位置时遇到了问题。
sprite.getBoundingRectangle().setPosition(x,y);
// then I check if it works
System.out.println(sprite.getBoundingRectangle().getPostion);
但结果总是0.0,0.0
答案 0 :(得分:1)
解决方案很简单。获取它的矩形并进行包含调用。
sprite.getBoundingRectangle().contains(new Vector2D(touch.x,touch.y)); //or
sprite.getBoundingRectangle().contains(touch.x, touch.y); //
确保您取消投影正确。