[java] How to create multiple instances of the same thing?

Started by
5 comments, last by CryoGenesis 12 years, 4 months ago
This is something I've struggled with and ended up bypassing with the two games I have made, but I figure I better learn it sooner than later, so any help is appreciated, I'll try and be more specific below:

What I'm trying to do is make multiple instances of the same thing, the most efficient way. (Ex: two of the same enemy on screen at once, or multiple collectibles on screen at once.)

How do I do this? My best guess would be making a class that has all of the info, but I don't know how to lay that out, or draw it to the screen. I know I'm not describing it well but I hope someone can help. Thanks!

Oh also, once I do draw them and everything, how do I destroy them?
Advertisement

This is something I've struggled with and ended up bypassing with the two games I have made, but I figure I better learn it sooner than later, so any help is appreciated, I'll try and be more specific below:

What I'm trying to do is make multiple instances of the same thing, the most efficient way. (Ex: two of the same enemy on screen at once, or multiple collectibles on screen at once.)

How do I do this? My best guess would be making a class that has all of the info, but I don't know how to lay that out, or draw it to the screen. I know I'm not describing it well but I hope someone can help. Thanks!

Oh also, once I do draw them and everything, how do I destroy them?


A class would probably make the most since to represent an enemy etc, what you need is something to contain the reference to each instance you create, an array for example.
As for what you store in that class, it depends on what you're representing. If it's an enemy you'd probably want to keep track of it's stats like health and attack power etc also more fundamentally it's location in on the screen and in your game map. You'd also functions to move it etc.

To destroy a class in Java you can either use delete e.g delete obj; and set the reference to NULL or destroy references to it by just setting it to NULL and the JVM's garbage collection will get to it at some point. The former is probably better than the latter. Then when you update/redraw the scene you only draw enemies in the array who's reference isn't NULL.
Patrick

[quote name='Tankyroo' timestamp='1322976718' post='4890317']
This is something I've struggled with and ended up bypassing with the two games I have made, but I figure I better learn it sooner than later, so any help is appreciated, I'll try and be more specific below:

What I'm trying to do is make multiple instances of the same thing, the most efficient way. (Ex: two of the same enemy on screen at once, or multiple collectibles on screen at once.)

How do I do this? My best guess would be making a class that has all of the info, but I don't know how to lay that out, or draw it to the screen. I know I'm not describing it well but I hope someone can help. Thanks!

Oh also, once I do draw them and everything, how do I destroy them?


A class would probably make the most since to represent an enemy etc, what you need is something to contain the reference to each instance you create, an array for example.
As for what you store in that class, it depends on what you're representing. If it's an enemy you'd probably want to keep track of it's stats like health and attack power etc also more fundamentally it's location in on the screen and in your game map. You'd also functions to move it etc.

To destroy a class in Java you can either use delete e.g delete obj; and set the reference to NULL or destroy references to it by just setting it to NULL and the JVM's garbage collection will get to it at some point. The former is probably better than the latter. Then when you update/redraw the scene you only draw enemies in the array who's reference isn't NULL.
[/quote]

Sorry for the late reply, I was busy yesterday.

I kind of get what you're saying, but I'm still confused. Could you give an example of what the class would look like?

Sorry for the late reply, I was busy yesterday.

I kind of get what you're saying, but I'm still confused. Could you give an example of what the class would look like?


This response makes it apparent that you don't quite yet have a basic understanding of what object oriented programming is.

That said, I'd read up a bit more on class structure in Java, and also hone your understanding of all the data types used in Java.

Only reason I'm assuming this is because I had the EXACT same problem when I didn't have a full understanding of OOP and data types, regardless of the language.

In my first game ever (written in C++), I was instantiating monsters by just saying something along the lines of:

monster = Monster(arguments)

And then just manipulating the object associated with that variable 'monster' throughout my entire game. I never really knew how else to store those monster objects, other than assigning each individual monster to a variable like this.

Instantiating a new monster is as easy as just saying Monster(arguments) if you already have a Monster class, but it ultimately comes down to your own understanding of object oriented programming and knowing data types that will allow you to do different things with this small 1 line of code. What prh99 said, using an array, is a good solution, but requires you to have an understanding of your class object (Monster in your case) and an array type of data structure.

Hope this helps.

-Adam

[quote name='Tankyroo' timestamp='1323114077' post='4890805']
Sorry for the late reply, I was busy yesterday.

I kind of get what you're saying, but I'm still confused. Could you give an example of what the class would look like?


This response makes it apparent that you don't quite yet have a basic understanding of what object oriented programming is.

That said, I'd read up a bit more on class structure in Java, and also hone your understanding of all the data types used in Java.

Only reason I'm assuming this is because I had the EXACT same problem when I didn't have a full understanding of OOP and data types, regardless of the language.

In my first game ever (written in C++), I was instantiating monsters by just saying something along the lines of:

monster = Monster(arguments)

And then just manipulating the object associated with that variable 'monster' throughout my entire game. I never really knew how else to store those monster objects, other than assigning each individual monster to a variable like this.

Instantiating a new monster is as easy as just saying Monster(arguments) if you already have a Monster class, but it ultimately comes down to your own understanding of object oriented programming and knowing data types that will allow you to do different things with this small 1 line of code. What prh99 said, using an array, is a good solution, but requires you to have an understanding of your class object (Monster in your case) and an array type of data structure.

Hope this helps.

-Adam
[/quote]
Thanks for the advice, do you have any suggestions as to where I should read-up on the topic? or is it all the same and I should just google an article on it?
I haven't exclusively studied Java yet, but I'd imagine you could find tons of documentation online about Java classes and types... more specifically usage examples which would probably give you a light bulb moment :P

public class Enemy{
int x,y;

public Enemy(int x, int y){
this.x =x;
this.y =y;
}


public void render(Graphics g){
//draw whatever here
}

}


public class main extends Applet{

Enemy[] enemy;
int numofenemys;

public void init(){
enemy = new Enemy[numofenemys];
for(int i =0; i < numofenemys; i++){
enemy = new Enemy(//insert random positions here)
}

}

public void paint(Graphics g){
for(int i =0; i < numofenemys; i++){
enemy.render(g);
}
}

}





So basically you can just group all the enemys up in an array and display and initialize them within a for loop.
The only thing i dont like about this method is that it can be hard to find out which enemy is which when displayed unless you declare a string inside the Enemy class.

Hope that helped!

Gen.

This topic is closed to new replies.

Advertisement