Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  An array filled with 2D arrays?  (Read 2464 times)
0 Members and 1 Guest are viewing this topic.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Posted 2012-04-10 02:04:16 »

So I have maps made of 2D arrays like so:

1  
2  
3  
4  
5  
6  
7  
8  
9  
int[][] level1 = {
         {4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
         {4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
         {4, 0, 1, 1, 4, 4, 4, 4, 4,}, //
         {4, 4, 4, 1, 1, 2, 3, 4, 4,}, //
         {4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
         {4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
         {4, 4, 4, 4, 4, 4, 4, 4, 4,}  //
   };


When I call the next level to be loaded, I access a getNextLevel class and pass the integer of the current level I am on. To make things easier on myself and not have to hard-code which level to load each time, I want to add allll of my levels into one array called Levels[], and then have  my getNextLevel class look something like this:

1  
2  
3  
public int[] getNextLevel(int level) {
      return levels[level];
   }


However, I could not get that to work. After researching on the idea, I couldn't find anyone else who had run into the same problem. Thanks for helping :3

-Nathan

Check out my website!
Online ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-04-10 02:06:30 »

Since your levels are 2D arrays, shouldn't getNextLevel also return a 2D array?

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #2 - Posted 2012-04-10 02:14:40 »

I guess so :l The part I am really flabbergasted on is whether my Levels array should be 2D or just a regular array. Since it is just holding a bunch of 2D arrays, I would think a regular array would work. However, when I try to make a new level and add it to Levels...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public int[] levels;
int[][] level1 = {
         {4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
         {4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
         {4, 0, 1, 1, 4, 4, 4, 4, 4,}, //
         {4, 4, 4, 1, 1, 2, 3, 4, 4,}, //
         {4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
         {4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
         {4, 4, 4, 4, 4, 4, 4, 4, 4,}  //
   };

public void createLevels() {
      levels[1] = level1;
   }


...the "level1" in create levels says "Type mismatch: cannot convert from int[][] to int" even though it isn't trying to use a standard integer anywhere. I even tried making levels a 2D array to match and it still said the same thing.

p.s. Want my source? Might help :l

-Nathan

Check out my website!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline _Al3x

Senior Devvie


Medals: 7


Indie Games FTW!


« Reply #3 - Posted 2012-04-10 02:21:30 »

levels[1] is especting an int, but you are giving an int[][] to it Sad

int levels[] means that levels is an array of integers, like this: [1][6][98][24], but level1 is a matrix, a 2 dimension array, like this:
[1][6][98][24]
[4][3][1][67]

Do you see it now? Hope it helps Smiley

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #4 - Posted 2012-04-10 02:22:24 »

Yeah I gotcha. So there's no way to do this? Sad_face.png

Check out my website!
Offline _Al3x

Senior Devvie


Medals: 7


Indie Games FTW!


« Reply #5 - Posted 2012-04-10 02:25:50 »

Yes you can, you have to create an array of 2d arrays, like your title:

int[][] levels[];

levels[1] now expects an array Smiley

BTW I haven't tested it, so... I'm not 100% sure, but it should work Smiley

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #6 - Posted 2012-04-10 02:27:14 »

Wow, can't believe I didn't try that. Thanks good sire, worked perfectly Smiley

-Nathan

Check out my website!
Offline _Al3x

Senior Devvie


Medals: 7


Indie Games FTW!


« Reply #7 - Posted 2012-04-10 02:30:47 »

You're welcome Smiley

Let us know about your progress!

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #8 - Posted 2012-04-10 02:32:27 »

Ok, side question: I am loading them all like this:

1  
2  
3  
4  
5  
6  
public void createLevels() {
      levels[1] = level1;
      levels[2] = level2;
      levels[3] = level3;
      levels[4] = level4;
   }


But is there some way to do it like this?

1  
2  
3  
4  
5  
public void createLevel() {
      for (int i = 1; i < 5; i++) {
         levels[i] = level[i];
      }
   }


So what I basically want to do is look through all of my level integers and find the one that has "i" for the ending. Doesn't seem possible, but maybe there is a way...

-Nathan

Check out my website!
Offline davedes
« Reply #9 - Posted 2012-04-10 02:51:28 »

Object-oriented patterns would definitely simplify/clarify your code and make things easier in the long run. Reading maps from a file or image would also help if you plan to have many levels.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
class Level {
    private int[][] tiles;

    public Level(int[][] tiles) { //or load it from a file just as easily
        this.tiles = tiles;
    }
}

List<Level> levels = new ArrayList<Level>();

public void init() {
    //create level 1
    levels.add(new Level( new int[][] { {0, 0, 0}, {1, 1, 1} .. etc } ));

    //create level 2
    levels.add(new Level( new int[][] { {1, 1, 1}, {2, 2, 2} .. etc } ));

    //starting level
    currentLevel = levels.get(0);
}

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #10 - Posted 2012-04-10 03:03:31 »

Wow. I wish I could hit appreciate 50 more times! I haven't done anything object-oriented before myself, so this is a lot of new stuff for me to see. Thanks so much to both of you.

-Nathan

Check out my website!
Offline sproingie

JGO Kernel


Medals: 202



« Reply #11 - Posted 2012-04-10 16:59:56 »

The important thing to notice about davedes's code above is that the tiles array is private, and you should get at it with accessors.  Don't worry about performance, because if all your getTile(x,y) method does is return tiles[ x ][ y ], it'll probably get inlined and be just as fast as direct access.
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.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (21 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (49 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (105 views)
2014-11-26 15:20:36

toopeicgaming1999 (31 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!