Creating many entities of an enemy or object from one method

For creating multiple boulders in my game, I’ve been doing this -

b1 = new Boulder(this);
b2 = new Boulder(this);
b3 = new Boulder(this);
etc...

This just doesn’t seem practical for games that have 10, 20, or even 50 things on the screen at once. Is there a sure-fire way to do this? I guess an example I have would be mobs in minecraft, I bet that for spawning new zombies and stuff it doesn’t do what I have been doing.

Thanks,
-Nate

You could create an array or something similar to hold all of the boulders and just add to it when you create a new one. To update and draw everything just iterate through the array.

Yeah a simple ArrayList that you add Boulders to and then loop through all of them to update and draw.

So I would just make an array called Boulders or something and… this is where I’m stuck :stuck_out_tongue:

What would I do next? Some example code would be helpful.

-Nathan

http://docs.oracle.com/javase/tutorial/
http://docs.oracle.com/javase/tutorial/collections/


List<Boulder> boulders = new ArrayList<Boulder>();

//Create some boulders
for(int i = 0; i < 3; i++) {
    boulders.add(new Boulder(this);
}

//Update
for(Boulder b : boulders) {
  b.update();
}

//Draw
for(Boulder b : boulders) {
   b.draw();
}

Others improvements would be a base class all you entities inherit from. have delta in your for smooth movement. Hope this helps

I don’t understand how I can update the coordinates for all of the boulders with this. What would the b.update method look like? And the b.draw method?

Thanks for the help.
-Nathan

That was a very simplified version of it i’ll admit but take for example update();

So if you created the boulders with different positions


for(int i = 0; i < 3; i++) {
    Boulder b = new Boulder(this);
    b.setX(i*10);
    b.setY(i*10);
    boulders.add();
}

Above would put them all in a diagonal line across your screen

Then for update Boulder would contain something like:


public void update() {
   x += 1; 
   y += -1;
}

of course in real world you would calculate velocity of the boulders instead of just move them across the screen.

Then to draw you would loop through them all like before and call draw:


public void draw(Graphics g) {
   g.drawImage(image,x,y,width,height);
}

Oh by update and draw i mean the update and draw in your game loop

Thanks! The thing that really confused me was that I didn’t know if you could use one variable for the x coordinate and 1 for y, or if each boulder had to have it’s own.

-Nathan

so long as the field is inside the Boulder class and belongs to the boulder it can be anything you want.

a List is simply a collection of Boulder instances all have the same class so anything you could do with:


boulder1 = new .Boulder(this);
boulder1.explode();
boulder2 = new Boulder(this);
boulder2.explode();

you can do with a List by looping through it


for(Boulder b : boulders) {
   b.explode();
}

One more question, what would the setX and setY methods look like?


public void setX(int x) {
  this.x = x;
}

public void setY(int y) {
  this.y = y;
}

Do yourself a favor and learn java properly. What you’re doing now will lead to endless trivial questions, while you could learn a lot faster by buying a good book.

Haha, thought the same thing.

http://math.hws.edu/javanotes/ a good free book online.