[java] Strange array problems

Started by
10 comments, last by Bimble Bob 17 years, 7 months ago
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.
It's not a bug... it's a feature!
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.
http://blog.protonovus.com/
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.
It's not a bug... it's a feature!
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.
It's not a bug... it's a feature!
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.
http://blog.protonovus.com/
tmpTile = LoAMap.Tiles[globalTileX][globalTileY];

should be

tmpTile = LoAMap.Tiles[globalTileX + i][globalTileY + y];
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
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.
It's not a bug... it's a feature!
Ok well that didn't work. Looks like it must be my graphics code then.
It's not a bug... it's a feature!
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
It's not a bug... it's a feature!
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&#<span class="java-number">47</span>;grass.jpg"</span>;<br>      tmpTile.State2Graphic = <span class="java-keyword">null</span>;<br>      tmpTile.foregroundGFX = <span class="java-keyword">null</span>;<br>      tmpTile.isWalkable = <span class="java-keyword">true</span>;<br>      tmpTile.isOverhang =<span class="java-keyword">false</span>;<br>      tmpTile.hasMultipleStates = <span class="java-keyword">false</span>;<br>      tmpTile.loopStates = <span class="java-keyword">false</span>;<br>      tmpTile.stateSwitchTime = <span class="java-number">0</span>;<br>      tmpTile.currentState = <span class="java-number">1</span>;<br>      tmpTile.offsetX = <span class="java-number">0</span>;<br>      tmpTile.offsetY = <span class="java-number">0</span>;<br>		<br>      <span class="java-comment">//Give each tile a default value</span><br>      <span class="java-keyword">for</span>(<span class="java-primitives">int</span> i = <span class="java-number">0</span>; i &lt; width; i++)	<br>      {<br>        <span class="java-keyword">for</span>(<span class="java-primitives">int</span> y = <span class="java-number">0</span>; y &lt; height; y++)<br>        {				<br>          tempTile<span style="font-weight:bold;">[y] = tmpTile;<br>        }<br>      }		<br>  		<br>      System.out.println(<span class="java-literal">"Map tiles have had default values set succesfully!"</span>);		<br>      <span class="java-keyword">return</span> tempTile; 		<br>    }  	 <br>    <span class="java-keyword">return</span> <span class="java-keyword">null</span>;  	 <br>  }<br><br><br></pre></div><!--ENDSCRIPT--><br><br>First, you are creating 1 instance of a tile and storing it in the reference to tmpTile.  You are then assigning that &#111;ne reference to every tile in the map.  So every tile is pointing to the same memory location.<br><br>Second, all those assignments that you do to tmpTile fields should be done in the constructor to Tile.<br><br>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.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement