This site requires JavaScript, please enable it in your browser!
Greenfoot back
monalay
monalay wrote ...

2015/11/28

three dimensional array to recreate a grid

monalay monalay

2015/11/28

#
can anyone help me? i want to recreate a grid, which i already build, but by using a new Class with an three dimensional array, which should run through the field and display the grid with its objects. the first dimension represents the cells in x-coordinates, the second dimension the cells in y-coordinates and the third should regive the objects which are in the cells. i'm now so far, but i'm stuck:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Methode, zum Erzeugen einer neuen Welt
    public void makeWorld(welt neuewelt)
    {
        neuewelt.setBackground(background);
        neuewelt.setOrigin(originX, originY);
     
     
        String[][][] objects = {
             
        }; 
            for (j = 0, j <= objects.length, ++j);
                for (n = 0, n <= objects.length, +++n);
                    for (i = 0, i <= objects.length, ++i) {
                        if(objects[j][n][i] && object[j][n][i].equals("suedwest")){
                        (neuewelt.addObject(new "suedwest",0,0) = newobject;
                        newObject.setCellLocaation(0,0);
                        (object[j][n][i] && object[j][n][i] = "suedwest");
                    } else {
                
  
                    }
Super_Hippo Super_Hippo

2015/11/28

#
With that exactly do you need help? Some things I see. A for-loop looks like this:
1
2
3
4
for (int i=0; i<someNumber; i++)
{
    //code to execute
}
It needs semicolons (;) and not commas (,). The semicolon at the end of line 11 ends the for-loop, so it doesn't do anything. Same in line 12. I am not sure if '++j' is working and I don't know what '+++n' does. Line 14:
1
objects[j][n][i]
does not return a boolean so it doesn't make sense to use it there in the if-condition. Line 15 starts with a '(', why? The world method 'addObject' needs a world object and two coordinates. You create an object with 'new ClassName(parameters)'. At the end of this line you want to set 'newobject' to... yes, to what? And so on... Before creating something 3D, you should start with the basics. There are so many things in your code which doesn't make any sense.
danpost danpost

2015/11/28

#
First, let's go over problems with the code: (1) the string array (lines 8 through 10) has no data (2) the 'for' statements (lines 11 through 13) all reflect the same part of the array (3) the 'for' statements each iterate one too many times (4) the iterator for the 'for' statements or quite unusual (line 12 should not compile) (5) the first condition in the line 14 'if' statement (before '&&') is not a boolean expression (6) line 15 sets 'newobject' to nothing (this should not compile) (7) line 15 tries to create a new Actor object out of a String value instead of calling an Actor subclass constructor (8) 'newObject', in line 16, is not defined (maybe you meant 'newobject' -- which is not defined either because of the previous issue) (9) the spelling of 'setCellLocaation' in line 16 is probably just a typo (though you could have named the method with that name) (10) all objects would be added (line 15) and located (line 16) at the same location -- (0, 0) (11) line 17 does not make much sense at all (I cannot for the life of me figure out what you intended that line to do) For (1), add data to the array. For (2) through (4), replace lines 11 through 13 with:
1
2
3
for (j=0; j<objects.length; j++)
    for (n=0; n<objects[j].length; n++)
        for (i=0; i<objects[j][n].length; i++)
Notice how each successive loop utilizes the previous interator values in determining the size of its particular part of the array. It does not make any difference if you use '++j' or 'j++' (they both process identically); but, '+++j' will not compile. Also, notice that I used '<' instead of '<='; For (5),
1
objects[j][n][i]
refers to a String value -- not a 'true' or 'false' value. For (6), the 'addObject' method has a 'void' return type and assignment statements always assign the result on the right side of the equal sign to a field or variable named on the left. Here, it appears the field or variable name is on the right side and, even if switched, would not be assigned any value because of the void return of what was on the left. There is also an unpaired round brace on that line. For (7), 'new "suedwest"' should not compile as a String value is not a class name. The contents of the string may actually be a string representation of the name of a class, but the compiler does not look at it as a class name. I think the rest are pretty self-explanatory. I feel they may be a better way of going about what you are trying to do; although it is totally unclear as to exactly what that is.
danpost danpost

2015/11/29

#
I do have one question that really needs to be answered before you continue with what you are doing. What do you intend to happen between the time that the array is loaded with data and the time that the data is used to recreate the world? The answer should be one of two things: (a) another world would be activated which in turn would recreate and reactivate the saved world; or (b) the scenario will be closed and reopened later -- recreating the world as it previously was; I will not presume that you have (or need) methods that will save the data from the array to a file and read the data from the file back into the array.
You need to login to post a reply.