0

オブジェクトの座標が要件 (目的地の座標) を満たしているかどうかを確認する必要があるゲームを作成しています。

例:

int x; //current object X coordinate
int y; //current object Y coordinate

int destinationX = 50; //example X destination  value
int destinationY = 0;  //example Y destination value
int permittedDiference = 5;

boolean xCorrect = false;
boolean yCorrect = false;

私はアルゴリズムを作成しようとしています、チェックしています

 if (x == destinationX + permittedDifference || x == destinationX - permittedDifference)
 {
     xCorrect = true;
 }

 if (y == destinationY + permittedDifference || y == destinationY - permittedDifference)
 {
     yCorrect = true;
 }

最も単純な方法のように聞こえますが、もっと良い方法があるのではないでしょうか? いくつかのヒントに感謝します。

4

1 に答える 1

5

Math.abs()ここでメソッドを利用できます。xとの間の絶対差を取得し、destinationXそれが以下であるかどうかを確認しpermittedDifferenceます。

xCorrect = Math.abs(x - destinationX) <= permittedDifference;
yCorrect = Math.abs(y - destinationY) <= permittedDifference;
于 2013-02-18T21:01:13.200 に答える