Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Setting up the Floor for 2d JAva Game?

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Thanked 0 Times in 0 Posts

    Default 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!

  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Warrington, United Kingdom
    My Mood
    Thanked 156 Times in 152 Posts

    Default 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.

    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.

    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.

    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

Similar Threads

  1. Programmer for a Java based game project
    By Takkun in forum Project Collaboration
    Replies: 4
    Last Post: June 14th, 2010, 05:47 PM
  2. Java hangman game help...
    By AnotherNoob in forum AWT / Java Swing
    Replies: 16
    Last Post: December 4th, 2009, 11:17 PM
  3. Help with 1st Java applet game!
    By Sneak in forum Java Applets
    Replies: 0
    Last Post: November 28th, 2009, 11:20 AM
  4. setting location of a dialog relative to the parent
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 1st, 2009, 05:42 PM
  5. Problems in setting classpath
    By missyati in forum Java Theory & Questions
    Replies: 3
    Last Post: June 30th, 2009, 12:43 AM