[SOLVED] Class array
hey guys, I am working on a Brick Breaker game (hit the ball with the paddle to break bricks)
I have made a Brick class, however I would like to use it as an array
How do I do this?
The Brick class:
The line I am using and need to modify to make an array:
public class Brick
static int state = 1;
static boolean alive = false;
I would like to reference it like:
static Brick brick = new Brick();
so that I have an array of 100 bricks, each with their own alive and state variables (and any others I add)
if (brick[i].alive = true)
I strongly advise you to make Brick a true OOP class and make the variables non-static instance variables. Otherwise if you change alive for one brick, you change them for all. This way it is simple to create an array of x number of Bricks.
I also recommend making alive private and use public getters / setters here though others may argue that for a small simple class this isn't as important as we make it out to be.
but syntax wise, how do I make it an array?
I changed the variables in the class to public like you suggested
To create an array of Brick you do it like an array of any other object:
int brickCount = 100;
Brick bricks = new Brick[brickCount];
for (int i = 0; i < bricks.length; i++)
// don't forget to construct each brick element
bricks[i] = new Brick();