0

In this, I'm testing if an int value is a whole number or has a decimal value, if it has a decimal, it slowly adds or subtracts to the value to make it a whole number. The first and the third parts work, but the second and fourth don't.

if(ax % tileSize != 0) {
    ax -= (ax % tileSize) / 6; // works fine
}
if(ax % tileSize != 0) {
    ax += (ax % tileSize) / 6; // doesn't work
}
if(ay % tileSize != 0) {
    ay -= (ay % tileSize) / 6; // works fine
}
if(ay % tileSize != 0) {
    ay += (ay % tileSize) / 6; // doesn't work
}

The ones that work are decreased by 48 / 6 each time, and the others should be increased by 48 / 6, but it seems that the amount they are increased by changes each time.

4

1 回答 1

1

鉴于此作者的评论:

这只是一个 Java 游戏,我要测试的是玩家的 x 坐标 (ax) 和 y 坐标 (ay) 是否与图块一致,因为这是基于图块的游戏。如果它们与图块不一致,则坐标会增加或减少,因此您与图块对齐。

做到这一点的方法是这样的:

double tileSize = 10;
double ax = 25;
double vectorX = Math.floor(ax/tileSize + 0.5) - ax/tileSize;

这将为您提供一个范围为 -1..1 的向量,您可以将其乘以速度或做任何您想做的决定来决定运动。例如:

ax = ax + Math.ceil(vectorX*speed);

y轴也是如此。另外,请注意我的公式中有双打,因此如果需要,请应用适当的演员表。

于 2013-11-13T07:03:40.330 回答