# Java multiple player objects/enemy objects?

NOTE: I'm a beginner and learning :D

Alright, so in my game I have a class for the player (with the parameters of x, y, color, width, height, and health, same parameters with the enemy). But I have a problem. So, in my player class, I have a draw method which sets the color and fills a rectangle with the variables specified in the parameters. In my main class, I create an instance of that player, and call the draw method from there, too. However, when I try to create more than 1 player object, it just renders the second one, rather than both, even if the x, y, color, and pretty much everything is different.

Basically, I can't draw more than 1 player to my screen. Same thing with the enemy.

• There will be a problem in your code as there's nothing fundamentally wrong with what you're saying. Could you quote a minimal set of code that replicates the problem. – Richard Tingle Apr 3 '16 at 18:48
• That said; have you declared your variables static? If you have; stop – Richard Tingle Apr 3 '16 at 18:49
• You're most likely drawing all of your player objects using the same position data, so they're drawing on top of one another - hence it looks like just one. – Engineer Apr 3 '16 at 21:43
• I'll try the other solution, if that doesn't work, I'll do what you asked and replicate the problem. Thanks for the help! – Miles Apr 4 '16 at 15:08
• By the way, I noticed something. When I try drawing another player object, it's twice as fast. Could it be something to do with the threads at all? – Miles Apr 4 '16 at 15:15

This is most likely because to make more than one instance of this you will need an array of these objects. Usually there would only be one player and multiple enemies so the player would be a single object and the enemies an array. Here's do simple code to explain this:

Enemy[] enemies = new Enemies[10];
for (int i = 0; i < enemies.length; i++)
{
enemies.render();
}
Player player = ...
player.render();


Or if you want multiple players the players variable would be an array as well.