1

我有一个类,旨在成为一个非常简单的游戏内关卡编辑器,用于由 Sprites 下的平铺图像组成的 2D 游戏。我正在调试的代码的目的是根据用户单击的位置更新 StringBuffer 中的字符,以更改在单击位置显示的图块。

程序将正在修改的字符串缓冲区用作地图,以识别应在哪个图块上绘制哪些图像。缓冲区中使用的每个 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)。在传递的变量和接收它们的方法之间没有调用我写的方法。

4

2 回答 2

2

非常怀疑这里发生了什么神奇的事情。选项:

  • 您的诊断错误
  • 您实际上并没有运行您认为正在运行的代码,例如由于构建已过时
  • 如果您使用 AOP 或其他修改字节码的东西,可能会产生影响

我会添加一些日志记录而不是依赖调试器。例如:

private class MouseInput extends MouseAdapter{
    public void mouseClicked(MouseEvent e){
        if(layer==0){
            int x = e.getX();
            int y = e.getY();
            log.info("About to modify geometry; x=" + x + "; y=" + y);
            modifyGeometry(x, y, currentTile);
        }
    }
}

和:

private void modifyGeometry(int x, int y, char tile) {
    log.info("Within modifyGeometry; x=" + x + "; y="+ y);
    ...
}

我会惊讶地发现这些日志行显示不同的值。

我还要补充一点,进一步除以 20 的业务非常令人困惑,并建议实际上您应该能够显着简化您的设计。

于 2012-08-25T20:06:07.263 回答
1

从您的断点开始,查看 modifyGeometry 的调用者,并追溯传入的 x 和 y 的派生。

如果您不知道如何执行此操作... 在调试视图中,您将看到一棵树,其中包含看起来像堆栈跟踪的内容。最初显示的行下方的行给出了调用者的姓名。单击该行,您将看到调用 modifyGeometry 的行。变量视图将显示调用者中的变量。

于 2012-08-25T20:01:39.190 回答