• Advertisement
Sign in to follow this  

2D Java game-deleting NPC's from an array

This topic is 1515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently working on a 2D java game. I'm saving each active NPC(each NPC has a class file, extedning an abstract NPC class) from the game in a list(array) and itterating it to display the NPC using one of it's methods. The problem that I've run into is that I can't find a way to delete the NPC from the list when it's HP level is <= 0. I'm sure there is probably a really simple way to do this that someone more experienced would be aware of.  

 

PS. This is my first 2D game XD

Share this post


Link to post
Share on other sites
Advertisement

When you say list( array ) - do you mean a list or an array?

 

If you are using lists - you should probably read up on them a bit, they are simple to understand and will only take a 10 minute read.

It would be a case of something like  'currentNPC.remove()' or something similar ( I dont use Java )

Check out - http://docs.oracle.com/javase/7/docs/api/java/util/List.html

 

If its an array, and you are doing something like NPC[10] and iterating through them, just move your dead enemy to the last array position, move all the rest up - and reduce the count in the update to something similar to 

int NPC_AMMOUNT = 10;

...

Enemy Dies - Reorder array

NPC_AMMOUNT--;

...

for( int i = 0; i < NPC_AMMOUNT; i++ ) {
    ... do rendering
} 

Maybe if your new to programming, you should try out the beginners boards :)

Share this post


Link to post
Share on other sites

Use an ArrayList. ArrayList has the add and remove method. Its size also dynamically changes when you add and remove elements from the ArrayList. You can change the type of the ArrayList to be of type object or interface.

 

Change the ArrayList to be of type interface is a good habit to have.

 

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

 

 

 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement