Reputation: 39
i'm attempting to create a map for a jrpg in java i'm working on as a learning project. I've made a 10x10 array on terrain objects called map, and created a terrain object, independent of the array, as a test. next i attempt to create a terrain object for each iteration of the map 2d array. here i get the error "terrain cannot be resolved to a variable" i have played around with moving a few lines, and done 30 or more minutes of google searches looking for a solution. any help or hints would be very much appreciated.
terrain[ ][ ] map = new terrain[10][10];
terrain T1 = new terrain();
public void mapp(){
int a = RPGmain.test.game.map.length;
int c=0;
int d=0;
while (c <= a){
int b = RPGmain.test.game.map[c].length;
while(d <= b){
terrain map[c][d]= new terrain(); // <------ the first terrain in this line is where i get the error
d++;
}
d=0;
c++;
}
}
Upvotes: 2
Views: 258
Reputation: 18320
When you declare a field or a variable you need to specify its type:
int d=0;
when you are assigning to already defined variable, you just need the name of that variable and new value:
d=0;
It's the same with arrays. You already declared map as:
terrain[ ][ ] map = new terrain[10][10];
you don't need to specify the type when assigning:
map[c][d]= new terrain();
Upvotes: 2
Reputation: 3037
You're better off using a for
loop when you know how many iterations you're going to need:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
map[i][j] = new Terrain();
}
}
Note also that, by convention, the names of classes begin with a capital letter. The compiler won't complain if you disregard this, but you'll thank yourself for it when you come to debug your code in future.
Upvotes: 1