Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I'm attempting to store objects in a multidimensional array in an attempt to save each position in a game boards 'state', however after the loop which is supposed to set each instance to it's own parameters they all end up the same variables. Did I not link the correct array or set it up wrong? There's also a lot of "square.something should be accessed in a static way".

Is square.var or World[x][y].var the correct way for referencing the objects variables?

public static void generateMap() {
    MapSquare[][] World = new MapSquare[10][10]; //2D array init.

    //Choose a square for the home position.
    Random homeRandom = new Random();
    int HomeX = homeRandom.nextInt(10);
    int HomeY = homeRandom.nextInt(10);

    //Chooses a key room.
    int KeyX = homeRandom.nextInt(10);
    int KeyY = homeRandom.nextInt(10);

    //Loop through the objects and set each's parameters.
    for (int i = 0; i < 10; i++)
        {
        for (int j = 0; j < 10; j++)
            {
            MapSquare square = new MapSquare();
            //World[i][j] = square;

            //Calculate the level of the square from the distance to home.
            int distX = HomeX - i;
            int distY = HomeY - j;

            int CalcX = Math.abs(distX); //Convert to positive if negative.
            int CalcY = Math.abs(distY); //Convert to positive if negative.

            //Generate contents of the square.
            int newRandom = random.nextInt(5) + 1;

            switch(newRandom) {
            case 1: // Spawn a monster only.
                {
                square.monster = true;
                square.treasure = false;
                square.trap = false;
                square.home = false;
                square.peekable = false;
                square.key = false;
                square.mapLevel = CalcX + CalcY;

                //Generate the monsters stats.
                Monster monster = new Monster();
                monster.setLevel(square.mapLevel);
                monster.setMaxHealth(monster.getLevel() * 5);
                monster.setHealth(monster.getMaxHealth());
                monster.setDamage(monster.getLevel() * 2);
                break;
                }
            }
            }
        }

    //Generate home square.
    World[HomeX][HomeY].monster = false;
    World[HomeX][HomeY].treasure = false;
    World[HomeX][HomeY].trap = false;
    World[HomeX][HomeY].home = true;
    World[HomeX][HomeY].peekable = true;
    World[HomeX][HomeY].key = false;
    World[HomeX][HomeY].mapLevel = 0;
    World[HomeX][HomeY].visited = true;
    }
share|improve this question
    
when you say random.nextInt(5) + 1 : you mean homeRandom.nextInt(5) +1 right ? In addition to that, I think you need to make your MapSquare class definition a little more standard. Modifying members directly is never a good idea. create setters for that. – Gyanapriya Mar 24 at 6:22
1  
Ah yes thanks, I see I defined another called random as well now which is redundant. – Fluidic Ice Mar 24 at 6:25
2  
Is this line //World[i][j] = square; supposed to be commented out? – Evan Knowles Mar 24 at 6:27
    
Yes that line is, being fairly new to this I was unsure and commented it out to see if anything changed which it didn't. MapSquare is basically only Getters and Setters right now other then init's. If I swap them to the Get's and Set's it says they should be accessed in a static way. – Fluidic Ice Mar 24 at 6:31
    
Don't tell me that you made everything static in the MapSquare class? If so, then that's your problem. Remove the static from each method and each field of that class, and put back that line that you commented out. Then, go and read up on what static means. – David Wallace Mar 24 at 6:40

1 Answer 1

The static keyword basically means that a method can be used independently of a specific instance of the class it is in. Such as the Math.abs(int i) function you are using, you don’t have to create an instance of the Math class to use it.

If you intend to use a class to store data it is generally a bad idea to include static methods as they would not be able to access that data.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.