スプライトの下のタイル画像で構成される 2D ゲーム用の非常に単純なゲーム内レベル エディターを意図したクラスがあります。私がデバッグしているコードの目的は、クリックした位置に表示されるタイルを変更するために、ユーザーがクリックした場所に基づいて StringBuffer 内の char を更新することです。
変更中の文字列バッファーは、どの画像をどのタイルに描画するかを識別するためのマップとしてプログラムによって使用されます。Buffer で使用される各 char 値は 1 つのタイルを表し、char の値によってそこに表示される画像が決まります。この目的のために、定数整数 (public static final int TILE_SIZE) を 40 に定義しました。私のタイルはすべて 40 x 40 ピクセルのサイズの正方形なので、この定数は、画像によって調整する必要があるものを計算するのに役立ちます。幅と高さ。
問題は、ユーザーが画面をクリックするたびに、メソッド内のコードが実行される前に、StringBuffer を更新するメソッドに渡されたマウスの x 値と y 値が 40 で除算されることです。
関連するコードは次のとおりです。
private void modifyGeometry(int x, int y, char tile){
//Levels are 20x20 grids
int xIndex=x;//The value of the index we will read based on x
int yIndex=y;//The value of the index we will read based on y
if(x<21*TILE_SIZE && y<21*TILE_SIZE){//For safety reasons, check that the x and y aren't somehow greater than the screen
xIndex=xIndex/TILE_SIZE;//Factor x down to which tile in the row it is on
yIndex=yIndex/TILE_SIZE;//Factor y down to which tile in the column it is on
if(xIndex>19){//We will need to reduce the value of x if its factor based on the TILE_SIZE is greater than 19 (a row is 0-19)
int storetemp=xIndex/20;
xIndex=xIndex-(20*storetemp);//Because xIndex is an int, xIndex/20*20 does not come out to xIndex, but instead only the first digit of xIndex (0-9)
}
if(y>19){
int storetemp=yIndex/20;
yIndex=yIndex-(20*storetemp);
}
}
int index=xIndex+yIndex;
this.geometry.setCharAt(index, tile);//This will set the character corresponding to the point the player clicked to the tile selected
}
...
private class MouseInput extends MouseAdapter{
public void mouseClicked(MouseEvent e){
if(layer==0){
modifyGeometry(e.getX(),e.getY(),currentTile);
}
Eclipse のデバッガーでいくつかのブレーク ポイントを使用して、e.getX() と e.getY() が座標を正しく取得していることを確認しました (私はそれについてほとんど疑いがありませんでした) が、int xIndex=x が通過する前に、x とy は定数 (40) で除算されています。私が書いたメソッドは、渡される変数とそれらを受け取るメソッドの間で呼び出されません。