# Setting up the Floor for 2d JAva Game?

• January 16th, 2010, 01:05 PM
DarrenReeder
Setting up the Floor for 2d JAva Game?
Hello... i am starting to make a few Java 2d games and so far i have learned how to make a person walk across the screen although I am not using floor tiles (like with mario..) i am just not letting the character go below a certain Co-ord....so i just wanna know how i start to make it so the character would stay on top of tiles?

any help with this would be great...thanks!
• January 19th, 2010, 06:36 AM
Json
Re: Setting up the Floor for 2d JAva Game?
Step 1 - The map
I guess you would have to have some sort of render system which will read a map file or a static map in memory that you've created. You could do this really simple, lets say there are two types of tiles, ones you can collide with and the ones you cannot.

Define a size for the tiles, lets say each tile is 32x32 pixels.

You then need to create the map/array. Lets say we have a normal java.util.Map for this.

Code :

`final Map<Integer, Map<Integer, Boolean>> map = new HashMap<Integer, Map<Integer, Boolean>>();`

So basically your map is a coordinate system, hence the Integer in the Map.

Step 2 - The collision detection
You now need a way to render this map of course but thats up to you. You then need to have a method that so and so often checks if the player is colliding with a tile.

Code :

`final Boolean collidable = map.get(0).get(0);`

Now that should get you tile for coordinate 0,0. It will return a boolean which will tell you if the tile is collidable or not.

Conclusion
This is a VERY simple model of how it could work and I'm sure you can make it ten times better than this but its fairly simple and if the game isn't that heavy on resources this should work for you. If you feel that you want to have more tiles than just the two you can of course swap the Boolean in the map for an Integer and then have a list of the defined tiles and what tile is collidable and not.

The basic idea is still there though. If you need any further help show us some code and I'll see if I can give you a hand.

// Json