我尝试设置边界矩形位置。
spr.getBoundingRectangle().setPosition(100,100);
Sytem.out.println(spr.getBoundingRectangle.getX() + " " + spr.getBoundingRectangle.getX());
// and the output is always 0,0(position)
我还尝试在条件== true时设置它的位置
if(justTouched == true){
spr.getBoundingRectangle().setPosition(100,100);
Sytem.out.println(spr.getBoundingRectangle.getX() + " " + spr.getBoundingRectangle.getX());
}// but the result is the same
最佳答案
边界 Rectangle
当您调用 getBoundingRectangle()
时生成和/或更新。这意味着您没有使用 Rectangle
的 setX 或 setY 来修改 Sprite 顶点。类(class)。而是使用Sprite.setPosition(x,y);
以这种方式移动 Sprite 会移动边界矩形。
还有以下方法可以更改 Sprite 的边界矩形(不要将此 setX 和 setY 与 Rectangle 类 setX 和 setY 混淆,这个方法会修改 Sprite 的顶点):
setX(x)
setY(y)
setBounds(x,y,width,height)
setSize(width,height)
translate(diffX,diffY)
translateX(diffX)
translateY(diffY)
最适合您的问题的方式如下。
spr.setPosition(100,100);
Rectangle sprBounds = spr.getBoundingRectangle();
Sytem.out.println(sprBounds.getX() + " " + sprBounds.getY());
关于java - 如何设置BoundingRectangle位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32934576/