Public Group

Help debug multidimensional array loop?

This topic is 2121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have a loop that's supposed to take two parameters of coordinates clicked on the screen and see if there is an item at that point. It's not working and I was wondering if I could get some help debugging it?

	public void clicked(int xClicked, int yClicked){
for(int ny = 0; ny < 7; ny++){
for(int nx = 0; nx < 4; nx++){
if(xClicked > x+(nx*65) && xClicked < x+(nx*65)+65 ){
if(yClicked > y-(nx*65) && yClicked < y-(nx*65)-65){
if(inventoryList[nx][ny] != null){
inventoryList[nx][ny].use();
}
}
}
}
}
}


It passes the xClicked check when I put in a output line for debugging, but won't pass the yClicked check. Any ideas>

Share on other sites

Try this:

if(yClicked > y-(nx*65)-65 && yClicked < y-(nx*65))

Share on other sites

Thank you, that sent me in the right direction. After adding that in and clicking a few different spots in my inventory bag, I found that it was triggering when I clicked just below my item. Now it works perfectly how I wanted it

if(yClicked > y-(nx*65) && yClicked < y-(nx*65)+65)


Thanks again.

Share on other sites

Here is an expanded version of Brother Bob's idea, in case you prefer to see more abstraction.  Note that all the named variables do not have to be decomposed the way I have shown them ... this is just to show all of the named concepts / values / partial values present in the domain you are working with.  In a real solution you would probably use about 1/3 to 1/2 of these actual values ... but which set depends on what is coming from your environment / configuration.  The main thing I want to encourage is - STOP USING MAGIC NUMBERS - put them in constants or #defines or something ... so that you can change them ONE PLACE and have your code still work.  So when you want to go from say 1280x800 to 1680x1050, or to a mobile devices 480x800 ... you can change you constants / config file, but not your debugged formulas / functions.

// Assuming these constant or configured values
int invMaxCols = 4;
int invMaxRows = 7;
int invCellWidth = 65;
int invCellHeight = 65;
int invGridOffsetX = ?;
int invGridOffsetY = ?;

// And these input values
int xClicked = ?;
int yClicked = ?;

// These can be easily computed - if needed
int invGridWidth = invMaxCols * invCellWidth;
int invGridHeight = invMaxRows * invCellHeight;
int invGridBoundsX = invGridOffsetX + invGridWidth;
int invGridBoundsY = invGridOffsetY + invGridHeight;

// AND THEN - version 1
bool isInGrid_v1 =
(xClicked >= invGridOffsetX && xClicked < invGridBoundsX
&& yClicked >= invGridOffsetY && yClicked < invGridBoundsY);

if(isInGrid_v1)
{
int cellX = (xClicked - invGridOffsetX) / invCellWidth;
int cellY = (yClicked - invGridOffsetY) / invCellHeight;
T selectedItem = inventoryList[cellX][cellY];
if(selectedItem != null)
selectedItem.use();
}

// OR - version 2 = almost same as Brother Bob's great and succint suggestion.
int gridX = xClicked - invGridOffsetX;
int gridY = yClicked - invGridOffsetY;
int cellX = gridX / invCellWidth;
int cellY = gridY / invCellHeight;

bool isInGrid_v2 =
(cellX >= 0 && cellX < invMaxCols
&& cellY >= 0 && cellY < invMaxRows)

if(isInGrid_v2)
{
T selectedItem = inventoryList[cellX][cellY];
if(selectedItem != null)
selectedItem.use();
}


• 13
• 18
• 29
• 11