Jump to content
  • Advertisement
Sign in to follow this  
Dom_152

[java] Strange array problems

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an two arrays of a Tile class I made for a Map Editor I'm making. One contains data on the tiles that can be seen and one contains data on tiles on the whole map. Now what I want to do is change the Graphic variable in one tile to something different. But what it does is it changes every tiles in that arrays Graphic variable to the new one. This happens in both arrays. I cannot figure out why. I am using this code to change the Maps Graphic:
LoAMap.Tiles[currGlobalTileX][currGlobalTileY].Graphic = "data/imgs/stone.jpg";
			LoAMap.Tiles[currGlobalTileX][currGlobalTileY].foregroundGFX = "data/imgs/rock.gif";
Then this code to update the visible tiles array:
//If the map has been made
if(mapMade)
{

  if(mapChanged)
  {			
     for(int i = 0; i < 16; i++)
     {
	for(int y = 0; y < 16; y++)
	{		
	  int globalTileX = ((centreTileX - i) + centreTileX) + 1;
	  int globalTileY = ((centreTileY - y) + centreTileY) + 1;				
					
 	  tmpTile = LoAMap.Tiles[globalTileX][globalTileY];
 					
	  Tiles[y] = tmpTile;						
							
	}
     }
					
     mapChanged = false;		
   }
}
Please help this is very frustrating! BTW Tiles[][] is the visible Tiles array and LoAMap.Tiles[][] is the map tiles array.

Share this post


Link to post
Share on other sites
Advertisement
You need to test/assert that tmpTile, and by association, globalTileX/Y and centreTileX/Y, are what you want/need them to be. With the limited code provided, I suspect that tmpTile is always the same for some reason.

Share this post


Link to post
Share on other sites
I have tested that, I inserted some System.out.println's to make sure globalTileX/Y were correct. I have also changed the globalTileX/Y to number e.g. [5][6] and I get the same result; all the tiles having the change applied.

Share this post


Link to post
Share on other sites
Even if I do this:


for(int i = 0; i < 16; i++)
{
for(int y = 0; y < 16; y++)
{
int globalTileX = ((centreTileX - i) + centreTileX) + 1;
int globalTileY = ((centreTileY - y) + centreTileY) + 1;

tmpTile = LoAMap.Tiles[16][25];

Tiles[4][4] = tmpTile;

}
}


It still applies it to all the tiles in the array and that's with integers instead of variables! I'm not sure if I might've inadvertently made it so taht all the Tiles are overwritten when they are drawn so i'l look in my GFXHandler. if it's not that then I'm completley clueless.

Share this post


Link to post
Share on other sites
Can you confirm that after map change the entire Tiles array is filled with the same tile? If not, then I suspect the problem might be in your rendering code.

Share this post


Link to post
Share on other sites
I'll try that, but don't I do that at the beginning of each loop where I re-define globalTileX/Y? I'll also start doing some debugging in my Graphics code.

Share this post


Link to post
Share on other sites
I've looked and looked and I really cannot find out what is causing this. I've put my code into a .RAR archive. Could someone take a quick look through and see if they can work out whats going on? Thanks.

Download here: http://rapidshare.de/files/33455861/MapEd.rar.html

Share this post


Link to post
Share on other sites
Your problem is here.


public Tile[][] createTileArray(int width, int height)
{
if(isMade)
{
Tile tempTile[][] = new Tile[width][height];
Tile tmpTile = new Tile();

System.out.println("Setting tiles to default values. " +
"This may take some time depending on " +
"the size of your map...");

tmpTile.Graphic = "data&#47;imgs&#47;grass.jpg";
tmpTile.State2Graphic = null;
tmpTile.foregroundGFX = null;
tmpTile.isWalkable = true;
tmpTile.isOverhang =false;
tmpTile.hasMultipleStates = false;
tmpTile.loopStates = false;
tmpTile.stateSwitchTime = 0;
tmpTile.currentState = 1;
tmpTile.offsetX = 0;
tmpTile.offsetY = 0;

//Give each tile a default value
for(int i = 0; i < width; i++)
{
for(int y = 0; y < height; y++)
{
tempTile[y] = tmpTile;
}
}

System.out.println("Map tiles have had default values set succesfully!");
return tempTile;
}
return null;
}




First, you are creating 1 instance of a tile and storing it in the reference to tmpTile. You are then assigning that one reference to every tile in the map. So every tile is pointing to the same memory location.

Second, all those assignments that you do to tmpTile fields should be done in the constructor to Tile.

I will try to post the corrections, but this bloody board is screwing up when I try to post some source code. But sometimes it lets me post source code.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!