11 Replies Latest reply: Oct 7, 2006 11:24 PM by 807607 RSS

    array question

    807607
      if i had an array for a map
      which held a tile object
           tile[][] = new Tile[10][10];
      which held varibles
           
             class Tile {
                public int terrain;
                public int x;
                public int y;
                public int speed;
           }
      would you assign the variables for each Tile
      like this >
           tile[1][1].terrain = 1;
           tile[1][1].x = 1;
           tile[1][1].y = 1;
           tile[1][1].speed = 20;
      
           tile[1][2].terrain = 3;
           tile[1][2].x = 1;
           tile[1][2].y = 2;
           tile[1][2].speed = 5;
      ect...
        • 1. Re: array question
          800322
          no, because I'm not really font of NullPointerExceptions, and I think there should be no public attributes.

          I'd do something like this
          - (nested for loop)
          tiles[x][y] = new Tile(x, y, Tile.TERRAIN_MOUNTAIN, 20);

          If they all have different values, then I'd try to read them from a file and pass the String to the c'tor, for it to parse what the terrain should look like.
          • 2. Re: array question
            807607
            so something like this?

            class Init {
                 Tile tiles[][];
                 String terrain;
                 int mapSize;
                 int speed;
            
                 public Init() {
                      int x;
                      int y;
                      this.mapSize = 10;
                      this.speed = 0;
                      for(x = 0; x < this.mapSize; x++) {
                           for(y = 0; y < this.mapSize; y++) {
                                this.speed++;
                                terrain = "Grass";
                                tiles[x][y] = new Tile(x, y, this.terrain, this.speed);
                 
                           }
                      }
                 }
            
                 public static void main(String[] arg) {
                      Init run = new Init();
                 }
            }
            
            class Tile {
                 int xPos;
                 int yPos;
                 String terrain;
                 int speed;
            
                 public Tile(int x, int y, String t, int s) {
                      this.xPos = x;
                      this.yPos = y;
                      this.terrain = t;
                      this.speed = s;
                 }
            }
            • 3. Re: array question
              800322
              Yepp. That way, you make sure each Tile is always correctly initialized in one single line, without the need for public attributes or setters. Common attributes have no business to be anything but private, and I think most of yours can also be final.
              • 4. Re: array question
                807607
                what does the NullPoinerException mean?

                because it gives me one line 16 and 22.

                Thanks
                • 5. Re: array question
                  807607
                  The way you were going to do it would have taken you hours : D
                  better to use loops .

                  By the way - how are you going to texture the tiles / fill with colour ?

                  im planning some wire frame model making soon
                  • 6. Re: array question
                    JosAH
                    I don't understand why the coordinates (x,y) of the (x,y)th tile are replicated
                    in the tile itself.

                    kind regards,

                    Jos
                    • 7. Re: array question
                      800322
                      what does the NullPoinerException mean?
                      You can read it up in the API, before asking. :) It means you're calling a mthod on a reference that points to null.
                      because it gives me one line 16 and 22.
                      Which are those?
                      • 8. Re: array question
                        800322
                        By the way - how are you going to texture the tiles /
                        fill with colour ?
                        Best: not at all. You want to read this: http://csis.pace.edu/~bergin/mvc/mvcgui.html

                        The tiles are abstract data. Defining what they should look like is not their business. Defining what should be presented is. Displaying it is the GUI's responsibility.

                        The texturing can be done by custom-painting images loaded from files using ImageIcon, btw.
                        • 9. Re: array question
                          807607
                          because it gives me one line 16 and 22.
                          Which are those?
                          tiles[x][y] = new Tile(x, y, this.terrain, this.speed);
                          and
                          Init run = new Init();
                          PS. Thanks for the link
                          • 10. Re: array question
                            807607
                            You didn't create the tiles array. It is null.

                            You need (in the Init constructor):
                            tiles = new Tile[mapSize][mapSize];
                            speed and terrain don't make much sense as member variables of the Init class. Declare them locally in the constructor. Then, don't pass "this.terrain" and "this.speed" to the Tile constructor--just pass "terrain" and "speed".
                            • 11. Re: array question
                              807607
                              Ok. i've got the concept working now just to put it into something usefull :)

                              Thanks everyone for the help.