Ok, so I am trying to create a level map, but I don't know how I should store it. Should I use an array for every level? I feel like that wouldn't be very efficient though. I'm not sure what I should do. Any thoughts?

EDIT: Currently I am storing the single map as a 2D Array, it looks like

int test_array[][] = { {...}, {...} }

I am iterating through each element and for every '1' or '2', draw a block. Is there a better way to do this?

  • 1
    \$\begingroup\$ Seems your asking us how you should form a data structure; but your not telling us anything about what is in that data structure. That makes it impossible to give a useful answer. \$\endgroup\$
    – Gnemlock
    Jan 17 '17 at 19:57
  • 3
    \$\begingroup\$ There are countless of options. There is no single best one, only the one which is best for your game. We don't know how your software architecture looks, so we can't help you with this decision. But if you elaborate more about what you tried and what problems you encountered, we might be able to help you. \$\endgroup\$
    – Philipp
    Jan 17 '17 at 19:58
  • \$\begingroup\$ pick a format. at the moment, Tiled is popular, and still worked on mapeditor.org/download.html \$\endgroup\$ Jan 17 '17 at 21:44
  • \$\begingroup\$ Thank you for suggesting that program. Ill give it a shot! Ill reply back later if it worked for me! \$\endgroup\$
    – shepgoba
    Jan 19 '17 at 2:14
  • \$\begingroup\$ So i've fixed it. I use a txt file and read them with a BufferedReader. \$\endgroup\$
    – shepgoba
    Jan 23 '17 at 0:28

If you are making a platformer or top down rpg style game an array can be very usefull.

As long as you can consistently convert from world coordinates to array coordinates (and back), you can easily iterate only through a subsection of the array that represents what is on the screen by controlling the start and end of the two loops you are using for iterating through the array.

I would recommend creating a "tile" or "square" object or enumerated that you store into the map array rather than ints. Each item in the enumerated list can have a different sprite variable, movement cost variable, ect.

That way you avoid having a bunch of if statements and having to keep track of what variables do what, you can just do testArray[y][x].drawImage(xgridSize,ygridSize); or something along those lines.

This will make it harder to edit your level in a text editor, but making a crude level editor won't take too long and is worth the time.

You might also consider making maps an object (and have them store their tile arrangement in an array). This can be useful, because there might be a bunch of information that you want to save in a level that wouldn't belong in the array.

For example, what level is next, where do enemies start and what enemies are there. You can roll all of that information into a map object.

  • \$\begingroup\$ I fixed it! I use a buffered reader to read the txt file i've created which holds the map content. \$\endgroup\$
    – shepgoba
    Jan 23 '17 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.