Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  2D Arrays, declaring globally?  (Read 645 times)
0 Members and 1 Guest are viewing this topic.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Posted 2012-01-15 22:31:39 »

I'm trying to make a single 2D array called "map" and have it store the data for the current level. However, when I try to make the 2D array at the beginning of the class like so -

1  
int[][] map;


it then gives me an error when I do this later on -

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
if (level == 1)
      {
         map = {
            {2, 2, 2, 2, 2, 2, 2, 2, 2,}, //
           {2, 2, 2, 1, 3, 3, 2, 2, 2,}, //
           {2, 2, 2, 2, 2, 2, 2, 2, 2,}, //
           {2, 2, 1, 3, 0, 3, 2, 2, 2,}, //
           {2, 2, 1, 2, 3, 2, 2, 2, 2,}, //
           {2, 2, 2, 2, 1, 1, 2, 2, 2,}, //
           {2, 2, 2, 2, 2, 2, 2, 2, 2,} //
        };
      }


The error is "Array constants can only be used in initializers". I tried the almighty google, and learned nothing.

The reason I want to do this is because I used to have it like this -

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
if (level == 1)
      {
         int[][] map = {
            {2, 2, 2, 2, 2, 2, 2, 2, 2,}, //
           {2, 2, 2, 1, 3, 3, 2, 2, 2,}, //
           {2, 2, 2, 2, 2, 2, 2, 2, 2,}, //
           {2, 2, 1, 3, 0, 3, 2, 2, 2,}, //
           {2, 2, 1, 2, 3, 2, 2, 2, 2,}, //
           {2, 2, 2, 2, 1, 1, 2, 2, 2,}, //
           {2, 2, 2, 2, 2, 2, 2, 2, 2,} //
        };
      }


But that is a local variable, and I can't use it later when it's needed here -

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
for (int x = 0; x < 9; x++)
         {
            for (int y = 0; y < 7; y++)
            {
               System.out.println(x + " x, " + y + " y");
               
               playerData = map[py][px];
               mapData = map[y][x];
           
               if (mapData == 0)
               {
                  px = x;
                  py = y;
               }
                                        etc... spawn boulders etc


Does anyone have any idea how I can do this without an error occurring?

The whole reason I want this is to have multiple levels, but only have one array it has to check. It can can see what the level is and set the array accordingly.

Here's my source - http://www.mediafire.com/?34c0uw48ap3pafk

Thanks,
-Nathan

Check out my website!
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-01-15 22:41:46 »

You can only do it that way immediately when declaring it. Your way, you need to add "new int[][]":
1  
2  
3  
4  
5  
map = new int[][] {
   { ..... },
   { ..... },
   .....
};

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #2 - Posted 2012-01-15 22:45:33 »

Oh Cry

So there's no way for me to use one 2D array for all of the different levels since I can't access the array from outside of that method?

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

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-01-15 22:49:34 »

Of course you can access the array from outside the method. What I meant was to use that initialization method with the curly braces, all you need to add is "new int[][]", that's all. Tongue

Offline ReBirth
« Reply #4 - Posted 2012-01-16 09:53:58 »

crazier way, add one more dimension representing level Grin

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.

Pippogeek (37 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (60 views)
2014-09-21 02:42:18

BurntPizza (30 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (52 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!