2D Array with ArrayList for map

So, here I am again…Feeling like a newbie. It’s been a while since i’ve coded anything and i’m trying to get back into it again…so i’m making a tower defense game. I’m just playing around right now with the map.

Basically, i’ve got a Level class and a Tile class. The tile class just holds the tile information like this:


public class Tile {

	private int tileType;
	private boolean walkable;
	private Vector2 tilePos;
	public Tile(int type, Vector2 pos) {
		tileType = type;
		tilePos = new Vector2(pos);
	}
	
	
public Vector2 getPos() {
	return tilePos;
}
	
public boolean isWalkable() {
	return walkable;
}


In my Level class, i’ve got a 2d array which determines which tile is where:


private void loadLevel(int width,int height) {
	int[][] levelData = new int[][] {
			{0,0,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,0,0,0,0}
	};
	

Simple…So what i’m trying to figure out, is how to actually keep track of the tiles if that makes sense. I’m going to put my render method in the level class and will iterate through the level to draw the maps…I was going to use a for loop and add each tile to an arraylist, then iterate through the arraylist in my render method to draw each tile in the correct place and the correct tile…But, I remember reading that an arraylist would be a bad way to do this and it just doesn’t seem like a good way…

I feel like i’ve taken one step forward and 10 back every time I take a break from java. I should be way more advanced than I am for as long as I have been messing around with java. Any suggestions or information would be greatly appreciated. I do well for a while, then get stuck and take a break, come back and am in the situation I am in now. Very frustrating…

Maybe that’s your problem. Take time to learn things properly rather than trying to do advanced stuff without understanding the basics.

As for your code problem, just put the tiles in a [icode]Tile[][/icode]. Or even better, keep them in an [icode]int[][/icode] like in your loadLevel method, and use the tile id to lookup the actual tile class in a map or something.

Definitely somebody I was hoping to hear from!

I haven’t really started into any of the advanced stuff yet though…That’s what i’m saying. I’ve made a couple small games like pong, breakout, and pacman…I shouldn’t be having these issues still…I don’t open eclipse for a couple months, and when I get started back in, I feel like I know less than I did…If that makes sense.

It’s the little things that get me…for example, I rewrote that using a List and i’m getting a null pointer exception when I add the tile to the list…which I set the title in the line directly above it, so it’s not null…


	for (int x = 0; x < 9; x++) {
		for (int y = 0; y < 9; y++) {
			Tile tempTile = new Tile(levelData[x][y],new Vector2(x,y));
			tileList.add(tempTile);

It wouldn’t throw an NPE if tempTile was null, it does because tileList is null.

That makes sense…Can’t believe I missed that. so I need to do something like

List tileList = new List();
This doesn’t seem to work…do I really need the ?

On a completely unrelated note…What would cause the compiler to run like complete shit after i’ve ran the game a couple times…It runs perfectly fine when I first start it up but as soon as i’ve run the game two or three times, it freezes up every time I do anything. Just moving the cursor around takes a few seconds. I think that’s a big part of my problem. It is extremely frustrating to debug like this.

You can’t create a ‘List’, because it’s just an interface. You have to use a class which extends that interface. Commonly used is ArrayList.


tileList =  new ArrayList();

List tileList = new ArrayList[b]<>/b;

As to slowness, are you terminating your games, or are the processes stacking up, taking up CPU?

Ah, I see. I could have googled that, I apologize for the dumb questions! Just a bitch to do when it’s running so slow. I’m not closing it or doing anything. It stops when it has the NPE error.

Depending on how your game is threaded, it quite possibly doesn’t terminate the process on NPE. Check the task manager for lots of java.exe processes.

Seems like a cool ‘‘newbie’’ (Should I say) project your working on, its a simular sort of question I’ve been meaning to ask. Please post the game when its finished! Especially with you working on tilemap arrays rather than using tiled etc. I love the whole idea of doing everything in Java.

And I know what you mean about taking steps back etc, I get the same feeling, but just enjoy learning thats what I always say. it’ll prob just get boring if you knew over 90%

May be something to do with the x/y order, although here it’s a square grid so I can’t see how. Remember though that to address a 2D array you specify the coordinates as y,x. Of course this is confusing so it’s clearer to create a class e.g. Grid or something which returns a Tile from its private 2D array based on a getter where you specify the x and y in the natural order. Makes for more readable code and fewer mistakes in the long run :smiley: (Edit: Hmm after consideration the order is probably arbitrary so long as one adopts one convention and sticks to it consistently, although in Java docs etc. the first dimension is always called the “row” which would correspond to y. Probably best to just ignore me ;D)
E.g. what I’ve tended to do is this:

Tile getTile(int x, int y) {return tiles[y][x];}

Don’t EVER do that, if you are confused as to what x and y are use row and col, or better yet make sure you imprint your mind with which direction is which.

I’d argue that that is better than row/col.

I like using x and y as it is what i’m used to from scripting on another game. It works well. Ive used tiled and its simple for maps. Its too much for what I need on this game though and id like to learn what it’s doing. I need the experience. I think ive figured out a good way to do this. When i get home, im going to continue working on it. Im hoping to be able to stay with it longer.

Ive got a much bigger project that i want to do but i am nowhere near ready…so im just trying to learn everything i can.

Technically the best solution is a flat array:


return tiles[y * width + x];

But then we’re heading into the realm of premature optimisations.

Or, depending on access patterns, a Hilbert/Z-order/etc curve-mapped array!

Best to simply know that there is no silver bullet.

i like

hashmap.get(vec2(x,y))

too. saves memory when there is lots of empty cells.

I’ve not even used hashmaps yet. Need to learn them next.

Okay…I got it working but it’s drawing everything rotated 90 degrees counter clockwise…

You’ve switched the x and y coordinates somewhere but not everywhere.