Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Creating many entities of an enemy or object from one method  (Read 1334 times)
0 Members and 1 Guest are viewing this topic.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Posted 2011-12-30 06:42:00 »

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

1  
2  
3  
4  
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

Check out my website!
Offline iCiNGG

Junior Newbie





« Reply #1 - Posted 2011-12-30 07:03:17 »

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.
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-12-30 08:26:35 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #3 - Posted 2011-12-30 20:10:47 »

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.


So I would just make an array called Boulders or something and... this is where I'm stuck Tongue

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

-Nathan

Check out my website!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 757
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2011-12-30 20:19:35 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline AbstractChaos

Senior Newbie


Medals: 2



« Reply #5 - Posted 2011-12-30 20:23:39 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
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
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #6 - Posted 2011-12-30 20:45:15 »

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

Check out my website!
Offline AbstractChaos

Senior Newbie


Medals: 2



« Reply #7 - Posted 2011-12-30 20:57:06 »

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
1  
2  
3  
4  
5  
6  
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:

1  
2  
3  
4  
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:
1  
2  
3  
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
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #8 - Posted 2011-12-30 21:05:59 »

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

Check out my website!
Offline AbstractChaos

Senior Newbie


Medals: 2



« Reply #9 - Posted 2011-12-30 21:10:21 »

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

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

1  
2  
3  
4  
boulder1 = new .Boulder(this);
boulder1.explode();
boulder2 = new Boulder(this);
boulder2.explode();


you can do with a List by looping through it

1  
2  
3  
for(Boulder b : boulders) {
   b.explode();
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #10 - Posted 2011-12-30 21:58:23 »

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

Check out my website!
Offline AbstractChaos

Senior Newbie


Medals: 2



« Reply #11 - Posted 2011-12-30 22:01:02 »



1  
2  
3  
4  
5  
6  
7  
public void setX(int x) {
  this.x = x;
}

public void setY(int y) {
  this.y = y;
}
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 757
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2011-12-30 22:25:08 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline tw0v

Junior Member


Medals: 1
Projects: 1



« Reply #13 - Posted 2011-12-31 00:29:35 »

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.

Programming for better games.
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #14 - Posted 2011-12-31 04:16:49 »

http://math.hws.edu/javanotes/ a good free book online.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (15 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (16 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!