views:

60

answers:

2

Ok so I have this 2D Tile RPG I am making.... here is a live applet: http://mystikrpg.com/so/

The code is also at http://mystikrpg.com/so/

Anyway, if you examine the code, it seems the I can only block tile 24 (which are the walls)...

Do any of you awesome wise java experts see any way I can have MULTIPLE tiles that are blocked?

I have zipped the required files so that you may play around if you want/can here: http://mystikrpg.com/so/2drpg.zip

I hope I can block more than one tile... :)

Thanks

A: 

Um, have a collection or array of tiles which are blocked, and change the blocked() method to see whether the given tile is in that collection?

A better way would be for the board not just to be integers, but references to instances of a Tile class which knows everything about that type of tile - its image, whether it's blocked, and anything else which is relevant. That way you can encapsulate the information about tiles in a single place - then ask for information about a particular tile in an easy way.

Jon Skeet
+1  A: 

Here's the code summary of your problem:

int[][] board; // row tiles for ultimate mapping experience!

private static int BLOCKED = 24;

public boolean blocked(int tx, int ty) {
  return board[ty][tx] == BLOCKED;
}

It clearly shows that you are only blocking "24". There are several approaches in which you can do this. Looking at your code, I agree with Jon Skeet that it's not very object oriented and it is better to use encapsulation and objects to help you in your code and design. But to answer your question, you can just simply put the blocked tiles in a HashSet and check if they contain the tile, then return it as "blockable". E.g.

private static final HashSet<Integer> BLOCKED_TILES = new HashSet<Integer>();
static {
  BLOCKED_TILES.add(24);
  BLOCKED_TILES.add(1);
  //add more tiles here
}

public boolean blocked(int tx, int ty) {
  return BLOCKED_TILES.contains(board[ty][tx]);
}
Manny