Reputation: 57
I'm trying to create a bunch of the same objects (Grass) with a loop in different spaces. I have a grid and I want it to fill up the entire grid with different colors. So I have this code so far:
public stage() {
super(null);
cast = new Cast();
cast.setBounds(10, 10, cast.getWidth(), cast.getHeight());
this.add(grid);
for (int i = 0; i <= 19; i++) {
obj = new Object[] {
new Grass (cast.cells[i][i])
};
}
}
This obviously doesn't work and only makes a colored cell in the last spot of the grid. Is there anyway to make a loop for objects in every spot?
Upvotes: 0
Views: 76
Reputation: 339
Your code is going to create an new Object[1] 20 times. That Array will contain an instance of Grass. Try this instead.
public stage() {
super(null);
cast = new Cast();
cast.setBounds(10, 10, cast.getWidth(), cast.getHeight());
this.add(grid);
Object obj[] = new Object[20];
for (int i = 0; i <= 19; i++) {
obj[i] = new Grass (cast.cells[i][i])l
}
}
}
Upvotes: 1