Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (534)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Add multiple maps  (Read 987 times)
0 Members and 1 Guest are viewing this topic.
Offline gmonti

Junior Newbie





« Posted 2012-11-10 03:20:48 »

http://www.youtube.com/watch?v=fn-T7J1ytMc&feature=fvwrel

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
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2012-11-10 03:24:00 »

I'm far far far too lazy to watch the video or look at the code... Roll Eyes
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.
Offline gmonti

Junior Newbie





« Reply #2 - Posted 2012-11-10 03:27:27 »

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline actual

JGO Coder


Medals: 23



« Reply #3 - Posted 2012-11-10 03:54:26 »

For the list, you would just need something like:
1  
2  
3  
4  
5  
6  
7  
8  
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);

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2012-11-10 04:15:16 »

For the list, you would just need something like:
1  
2  
3  
4  
5  
6  
7  
8  
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.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #5 - Posted 2012-11-10 04:16:40 »

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?
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.
Offline gmonti

Junior Newbie





« Reply #6 - Posted 2012-11-10 04:51:51 »

I am lost. I don't know how to implement the code you gave me. This is my first java game.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #7 - Posted 2012-11-10 05:11:07 »

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:
1  
2  
3  
4  
public class Game {
ArrayList<Map> maps = new ArrayList<Map>();
...
}


This creates an ArrayList (http://www.java-samples.com/showtutorial.php?tutorialid=234).
1  
...<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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
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).



Offline Agro
« Reply #8 - Posted 2012-11-10 05:24:06 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
// 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 Smiley.

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #9 - Posted 2012-11-10 05:54:46 »

Or you could do it Agro's way. You like identifiers, I like loops  Roll Eyes
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gmonti

Junior Newbie





« Reply #10 - Posted 2012-11-11 12:20:27 »

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

1  
2  
3  
4  
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.
Offline ctomni231

JGO Wizard


Medals: 98
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #11 - Posted 2012-11-11 16:12:15 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
...

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


Also...

1  
2  
3  
4  
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...

1  
2  
3  
4  
5  
6  
7  
...

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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (35 views)
2014-07-24 01:59:36

Riven (33 views)
2014-07-23 21:16:32

Riven (21 views)
2014-07-23 21:07:15

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!