0

Basically, I'm having trouble loading levels in my game due to a problem with loading the tiles. What I can't figure out is how to have the program save the tile's type as a string, such as "water" or "grass", and load the tile from those strings. Here is an example of the tiles code:

Example = new TileType("Texture.png", Material.Mat, True, True, "Example");

The first value is the Texture location, the second is the material, the third is whether or not the tile can be destroyed, the fourth is whether or not NPCs can pass over the tile, and the last is what the tile is saved as in the level data.

Here is what the game saves the tiles as:

<tile x="0" y="0" type="Example" />

What I am trying to do is make a method that takes the string and finds the tile that uses it. Can anyone help me with this?

5
  • 3
    How about using a Map<String, TileType>? Commented Jul 30, 2012 at 4:46
  • Make that answer Matt - or else I will ;) Commented Jul 30, 2012 at 4:49
  • @MattBall I'm with AmirAfghani ;) Commented Jul 30, 2012 at 4:51
  • @AmirAfghani & MadProgammer: it is done. Commented Jul 30, 2012 at 4:54
  • If you are serializing your objects as XML, you need an XML parser to read them in again. Try dom4j's DocumentHelper.parseText() Commented Jul 30, 2012 at 4:55

4 Answers 4

2

How about using a Map<String, TileType>?

(Posting comment as answer, as requested.)

Sign up to request clarification or add additional context in comments.

Comments

0

1. Better use Collection like Map.

2. Consider it like this... HashMap<String, TileType>

Comments

0

I would make a class called Tile.

public class Tile
{
    private Material mMaterial;
    private boolean mCanBeDestroyed;
    private boolean mCanBeWalkedOn;
    private String mLevelSaveName;

    // getters and setters
}

Then have something like this to access them:

private HashMap<String, Tile> mTiles;

Comments

0

For the number variables, you can use

String s = readIntFromFile(); //Read a number
try
{
    int x = Integer.parseInt(s); //Get an integer from the file
}
catch(NumberFormatException e)
{
    //if parseInt fails, we end up here
}

For the strings, here's one idea:

static final String[] tileTypes = {"Example", "Example2"};
private static int findTileType(String type)
{
    for(int i = 0; i < tileTypes.length; i++)
    {
        if(type.equals(tileTypes[i])) return i;
    }
    return -1;
}

public TileType makeTile(String type)
{
    int t = findTileType(type);
    switch(t)
    {
        case 0: 
            return example;
        case 1: 
            return example2;
    }
    return null;
}

static final TileType example = new TileType(/*...*/);
static final TileType example2 = new TileType(/*...*/);

Well, you get the idea. Probably not the best solution, but it would work.

Edit over a year later: Whoops, forgot to make findTileType static.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.