Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  2D Arrays, declaring globally?  (Read 279 times)
0 Members and 1 Guest are viewing this topic.
Offline StonePickaxes

Full Member
**

Posts: 202
Medals: 3


Nathan Kramber


« on: 2012-01-15 17: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
*****

Posts: 3131
Medals: 195


I'm the King!


« Reply #1 on: 2012-01-15 17: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

Full Member
**

Posts: 202
Medals: 3


Nathan Kramber


« Reply #2 on: 2012-01-15 17: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! Go get 'em!
Offline ra4king

JGO Kernel
*****

Posts: 3131
Medals: 195


I'm the King!


« Reply #3 on: 2012-01-15 17: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

JGO Wizard
****

Posts: 1263
Medals: 19



« Reply #4 on: 2012-01-16 04:53:58 »

crazier way, add one more dimension representing level Grin

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.165 seconds with 20 queries.