This content has been marked as final. Show 3 replies
deathwings wrote:I would use an [ArrayList |http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html] that holds the active aliens or bullets. When you want to remove them, simply call the remove( ) function. Then in your paintComponent method (which I don't see here), just draw any aliens or bullets in the ArrayList.
Creating Space Invaders game and im getting confused on removing objects from my game. How do you actually remove an object (i.e bullets and aliens).
Alternatively, you could use your arrays and just set the aliens or bullets to null when you don't want them around anymore. Just make sure whenever you're looping through your arrays that you check for null then.
Yet another option is to have a boolean value inside your aliens and bullet classes that keeps track of whether or not that object is active.
I'd go with the first option though.
deathwings wrote:Not a problem.
I was thinking bout that arraylist angle before, but it makes sense now so i think that will work ok. Thx kevin.
Taking the List idea one step further, you could have a parent Object/Interface that all of your game objects extend/implement. Let's call it GameObject.
A GameObject would have a draw(Graphics g) function that you call from your paintComponent( ) method, and for example an act( ) function as well.
You'd have a single List of GameObjects that you add everything in your game into. Now painting is simply a matter of looping over a single List. And in your game logic loop, you could do a similar thing with the act( ) function: your bullet's act( ) would simply move it forward and check for collisions and going off screen. An alien's act( ) function would move it around and maybe shoot a weapon every so often.
Just an idea.