Add multiple maps

So I was following this tutorial and made some modifications to it. What I want to do now is feature 3 sets of different maps that I can play right after the other. Can anyone help me? This is my Map class.

http://pastebin.java-gaming.org/581404d0e32

I’m far far far too lazy to watch the video or look at the code… ::slight_smile:
but it seems like you need a map cycle. Why not just use an ArrayList or array or list or something to hold a sequence of maps, and then when you are done playing a map just set the current map to the next one.

Hi Mr Jimmt,

Thank you for the fast reply. The code is only 70+ lines long. I don’t know how to do the Arraylist that you are recommending. Do I need to build another class? Like MapList or something like that?

For the list, you would just need something like:


List<Map> maps = new ArrayList<Map>();

//  Add your maps to the list
maps.add(new Map());
maps.add(new Map());

// Get the first map
Map currentMap = maps.get(0);

The other main change you are going to have to make is in your Map class. It looks like you have file paths and whatnot hardcoded iinto the class. More of these parameters are going to have to be passed in somehow, for instance via the constructor.

So instead of new Map() you will have something like new Map(mapFile);

Absolutely, because if you want your “Map” class to be a universal class for all maps then you need to be able to specify the type of map when you instantiate a Map, compared to something like “map.file = load(”…/…");" which is obviously very clumsy.

It’s usually not necessary to create your own data types unless you want to play around with performance. ArrayList already allows you to specify a specific type of object that it can hold.

I am lost. I don’t know how to implement the code you gave me. This is my first java game.

Ok, let’s try to make this simpler for you…

According to the pastebin dump you have your “Map” class. It has several pieces of data like the images. And the map itself is defined by an array of the String type. Good so far?

So, I’m assuming you have a “Game” class of some sort, that displays the map. Inside that class:


public class Game {
ArrayList<Map> maps = new ArrayList<Map>();
...
}

This creates an ArrayList (http://www.java-samples.com/showtutorial.php?tutorialid=234).

...<Map>...

This means that the ArrayList can hold objects of the Map class that you created. Now I’m assuming that you have a way of detecting when the game ends, so here’s some pseudocode:


if(gameEnd){
for(int i = 0; i < maps.size(); i++){
     if(maps.get(i) == currentMap){
          if(i + 1 < maps.size()) //can't request the fifth index of a list if the list in only four items long, for example
               currentMap = maps.get(i + 1);
          else //this is set off if the last map in the list is reached, aka, the index (i) of the map is the last one of the list
               currentMap = maps.get(0); //if the last map is reached then we go back to the first one
}}
}

So basically, if the game is detected to be ended, then the current map is set to the next map in the list. The “for” loop is just to determine what map is currently running. The current map is set to the next map by using getting the next index (i + 1).

Its actually really easy. All you need is some sort of collection class, like ArrayList, as Jimmt said above. You add all your maps you want in your game to that ArrayList. Then you should have an integer that contains the map ID of the current level. Once you know you finish a current level, you can add some sort of transition, and then just increase that integer. This is the most simplest way I would do it.


// definitions
ArrayList<Map> maps = new ArrayList<Map>();
maps.add(new Map(0));
maps.add(new Map(1));
maps.add(new Map(2));

int curmap = 0;

///////////// game logic + renderinng

while(gamerunning) {
maps.get(curmap).update();
maps.get(curmap).render();

if(maps.get(curmap).isCompleted()) {
    curmap++;
}
}

You can modify this template to have it any way you want, like actually having the Map class as a variable instead of referencing to the ArrayList to retrieve the map every time. I like to do things by identifier values :).

Or you could do it Agro’s way. You like identifiers, I like loops ::slight_smile:

hi everyone. i still can’t get it to work. whenever i add the

ArrayList<Map> maps = new ArrayList<Map>();
maps.add(new Map(0));
maps.add(new Map(1));
maps.add(new Map(2));

to my Board class (which paints the map class), it returns an error “package maps does not exist”.

Please help.

Uhhh… what Map class are you using now? If you are using the exact same code you were using before, then you will run into problems.


...

public Map() {
        ImageIcon img = new ImageIcon([REDACTED]);
        grass = img.getImage();
        img = new ImageIcon([REDACTED]);
        wall = img.getImage();
...

Also…


ArrayList<Map> maps = new ArrayList<Map>();
maps.add(new Map(0));
maps.add(new Map(1));
maps.add(new Map(2));

this was just placeholder code to help you to create new Maps into an ArrayList. Your map class will need to have a constructor…


...

public Map(int value) {
//insert stuff here
}

...

… in order for the above code to work. I think in your case you should re-post what code you are using now so we have a better idea of what is going on. If you can do the full code for both Board.java and Map.java, that’ll be really helpful.