我正在使用LibGdx,并尝试将他们的Rectangle类作为按钮在触摸屏上按下的边界。在1:1比例下它完美地工作,但是当我把游戏放到我的手机上(屏幕更小)时,图像被缩放和绘制正确,但矩形不会。所以我尝试保持我的矩形在它们正常的比例下,并“放大”触摸屏的XY坐标,但我猜我没有做对,因为它不起作用。
optionsMenu = new Vector<Rectangle>();
optionsMenu.add(new Rectangle(100 + (120 * 0), 100, 100, 100));
optionsMenu.add(new Rectangle(100 + (120 * 1), 100, 100, 100));
optionsMenu.add(new Rectangle(100 + (120 * 2), 100, 100, 100));
optionsMenu.add(new Rectangle(100 + (120 * 3), 100, 100, 100));
这是我初始化边界矩形的方式。
这是我初始化相机的方式:
camera = new OrthographicCamera();
camera.setToOrtho(true, 800, 480);
这是我绘制按钮的方法:
spriteBatch.draw(buttonImage, optionsMenu.get(2).bounds.getX(),
optionsMenu.get(2).bounds.getY(), optionsMenu.get(2).bounds.getWidth(),
optionsMenu.get(2).bounds.getHeight(), 0, 0, buttonImage.getWidth(),
buttonImage.getHeight(), false, true);
这就是我如何处理触摸屏逻辑的方法:
public boolean tap(float x, float y, int count, int button) {
Vector3 temp = new Vector3(x, y, 0);
camera.unproject(temp);
float scalePos = (int) ((float) Gdx.graphics.getWidth()/800.0f);
temp.x = temp.x * scalePos;
temp.y = temp.y * scalePos;
if(optionsMenu.get(0).bounds.contains(temp.x, temp.y)){
//do sutff
}
else if(optionsMenu.get(1).bounds.contains(temp.x, temp.y)){
//do other stuff
}
return false;
}