Java When I pass a Parameter to this Method, it is erroneously divided by one of my Constants before any code in the method executes

StackOverflow https://stackoverflow.com/questions/12125211

  •  28-06-2021
  •  | 
  •  

Question

I have a class that is meant to be a very simple in-game level editor for a 2D game composed of tiled image under Sprites. The purpose of the code I am debugging is to update a char within a StringBuffer based on where the user clicks in order to change what tile is displayed in the position the clicked.

The String Buffer being modified is used by the program as a map to identify which images should be drawn at which tile. Each char value used in the Buffer will represent 1 tile, with the value of the char deciding which image is displayed there. To this end I have defined a constant integer (public static final int TILE_SIZE) to 40. My tiles are all squares with a size of 40 by 40 pixels, so this constant is used to help calculate anything that needs to be adjusted by the image's width and height.

The problem is this: whenever the user clicks on the screen, the x and y value of the mouse passed to the method that updates my StringBuffer are divided by 40 before any code in the method executes.

Here is the relevant code:

    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);
        }

I have used some break points in Eclipse's debugger to determine that e.getX() and e.getY() are getting the coordinates right (I had little doubt of that), but before int xIndex=x even goes through, both x and y have been divided by the constant (40). No method I wrote is called in between the variables being passed and the method receiving them.

Was it helpful?

Solution

I very much doubt that anything magical is going on here. Options:

  • Your diagnostics are wrong
  • You're not actually running the code you think you're running, e.g. due to the build getting out of date
  • If you're using AOP or something else which modifies bytecode, that could have an effect

I'd add some logging instead of relying on the debugger. For example:

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);
        }
    }
}

And:

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

I'd be amazed to find those log lines show different values.

I'd also add that the business about further dividing by 20 is extremely confusing, and suggests that actually you should be able to simplify your design significantly.

OTHER TIPS

From your breakpoint, look at the caller of modifyGeometry, and trace back the derivation of the passed-in x and y.

If you don't know how to do this... In the Debug View, you'll see a tree that includes what will look something like a stack trace. The line below the one that's originally displayed gives the name of the caller. Click on that line and you'll be shown the line in which modifyGeometry is called. The Variables view will show you the variables in the caller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top