Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
3 replies to this topic

#1   Members   -  Reputation: 130

Like
0Likes
Like

Posted 27 November 2013 - 11:56 AM

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



#2   GDNet+   -  Reputation: 1567

Like
1Likes
Like

Posted 27 November 2013 - 12:07 PM

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 :)



#3   Prime Members   -  Reputation: 4324

Like
0Likes
Like

Posted 27 November 2013 - 12:20 PM

Set that array slot to Null; When rendering\updating, if that slot holds Null, don't do anything.

 

http://stackoverflow.com/a/17302320



#4   Prime Members   -  Reputation: 1535

Like
1Likes
Like

Posted 27 November 2013 - 05:59 PM

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

 

 

 






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS