Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

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

1 Answer 1

up vote 2 down vote accepted

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) {
    if(player.collidesWith(enemy))
        //Do something
}

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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