为什么 Canvas 的 Rect 在改变它的 y 坐标时会上下移动?
Why does Rect from Canvas move up and down when changing it's y coordinates?
我正在用 Canvas 制作一个简单的 2d 游戏。我有一些矩形从屏幕顶部掉落。当我减少每秒的帧数时,我可以看到当我增加矩形的 y 坐标时,矩形会上下移动一点点。
这就是我移动矩形的方式:
public void incrementY(float y) {
rectangle.top += y;
rectangle.bottom += y;
}
我只是将矩形的顶部和底部增加一个浮点数以将其向下移动。我移动矩形的方式有问题吗?为什么矩形在本应向下移动时向上移动?
您的坐标是 float
,但像素是 integers
。使用 strict rounding
的某些情况将坐标转换为像素,例如天花板:
public void incrementY(float y) {
rectangle.top = Math.ceil(rectangle.top + y);
rectangle.bottom = Math.ceil(rectangle.bottom + y);
}
我正在用 Canvas 制作一个简单的 2d 游戏。我有一些矩形从屏幕顶部掉落。当我减少每秒的帧数时,我可以看到当我增加矩形的 y 坐标时,矩形会上下移动一点点。 这就是我移动矩形的方式:
public void incrementY(float y) {
rectangle.top += y;
rectangle.bottom += y;
}
我只是将矩形的顶部和底部增加一个浮点数以将其向下移动。我移动矩形的方式有问题吗?为什么矩形在本应向下移动时向上移动?
您的坐标是 float
,但像素是 integers
。使用 strict rounding
的某些情况将坐标转换为像素,例如天花板:
public void incrementY(float y) {
rectangle.top = Math.ceil(rectangle.top + y);
rectangle.bottom = Math.ceil(rectangle.bottom + y);
}