0
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Richard Tingle Apr 3 '16 at 18:48
  • \$\begingroup\$ That said; have you declared your variables static? If you have; stop \$\endgroup\$ – Richard Tingle Apr 3 '16 at 18:49
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Engineer Apr 3 '16 at 21:43
  • \$\begingroup\$ 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! \$\endgroup\$ – Miles Apr 4 '16 at 15:08
  • \$\begingroup\$ 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? \$\endgroup\$ – Miles Apr 4 '16 at 15:15
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.