Sign in to follow this  

[java] Strange array problems

This topic is 4107 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[i][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
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
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[i][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
This is what you need. (Ok it works if I use pre instead of source)

//************ from map class ****************
public Tile[][] createTileArray(int width, int height) {
if(isMade) {
Tile tempTile[][] = new Tile[width][height];

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

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

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

//*********** tile class *****************************************************
public class Tile {
//Tile variables used by the MapHandler, GFXHandler and PlayerHandler
//To determine actions
//The tiles graphics
String Graphic;
String State2Graphic;
String foregroundGFX;
//Is the tile walkable?
boolean isWalkable;
//Is the tile an overhang causing the player to go "underneath" it?
boolean isOverhang;
//Does this tile have two different states? E.g. A door with opened and closed
boolean hasMultipleStates;
//Should the tile automatically loop through the difefrent states?
boolean loopStates;
//If so what should the timing be between switches?
int stateSwitchTime;
//What is the tiles current state?
//DEFAULT 1
int currentState = 1;
//X and Y co-ordinates of the tile
//Used only for scrolling the tiles
//These co-ordinates are relative to the real co-ordinates
//E.g. X += 0.5 would move the tile Right by half a tile.
int offsetX, offsetY;

//NOTE: If the states are not looped the user can manually switch state in the event() method
// By simply changing the value of currentState


public Tile() {
Graphic = "data/imgs/grass.jpg";
State2Graphic = null;
foregroundGFX = null;
isWalkable = true;
isOverhang =false;
hasMultipleStates = false;
loopStates = false;
stateSwitchTime = 0;
currentState = 1;
offsetX = 0;
offsetY = 0;
}
//Abstract Event method. This method should be overwritten by a user defined action.
//It is used by the Event handler in order to carry out specific tasks when a tile is "used"
//If the tile needs no event the user need only leave the metohd empty.
public void event() { }

}

Share this post


Link to post
Share on other sites
Ah! It seems so obvious now! Thank you! Now I can learn from my mistakes. It can only make me better. Ha it stills screws up but thats probably from all the messing around I've done with it.

Share this post


Link to post
Share on other sites

This topic is 4107 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this