I'm making my first game in Java, a top down 2D RPG. I've handled basic collision detection, rendering and have added an NPC, but I'm stuck on how to handle interaction between the player and the NPC.

Currently I'm drawing out my level and then drawing characters, NPCs and animated tiles on top of this. The problem is keeping track of the NPCs so that my Character class can interact with methods in the NPC classes on collision.

I'm not sure my method of drawing the level and drawing everything else on top is a good one - can anyone shed any light on this topic?

  • \$\begingroup\$ That's typically how drawing is done - in layers. \$\endgroup\$ – Vaughan Hilts Jun 29 '13 at 16:37
  • \$\begingroup\$ Okay thanks, I'm new to game development and although it might seem obvious to an experienced game dev some things just puzzle me! \$\endgroup\$ – James Williams Jun 29 '13 at 16:50

I wouldn't be afraid to just keep track of the enemies in an ArrayList or something and loop through the enemies to check explicitly if they collide.

for(Enemy enemy : enemiesArray) {
        //Do something

Unless you have a lot of enemies or you're crammed for performance, I don't see anything wrong with this approach.

|improve this answer|||||
  • \$\begingroup\$ Thanks, that clears it up for me. I think I'll only have 3 or 4 NPCs or enemies at a time anyways. \$\endgroup\$ – James Williams Jun 29 '13 at 16:52

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.