Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

how to restart a game? problem with my code is that when it restarts it double the enemy. so there are 10 enemies and lets say player dies than user restart and now there are 20 enemies and so on.....

in start method i am create 10 enemies and storing in arraylist

public void start()
for(int i = 0; i < 10; i++){  
    enemyObject = new Enemy(10*i,10);
    enemyStore.add(enemyObject);    //store in array

in here i am making enemy move, collision etc and if enemy is dead than i am remooving it from arraylist

public void actionPerformed(ActionEvent e)
    for(int i = 0; i < enemyStore.size(); i++){       
        enemyObject = (Enemy)enemyStore.get(i);
            if(!enemyObject.getDead()){               //if enemy is not died
                //make enemy move, collision etc... here
        else{           //remove enemy and create another one
            enemyObject = new Enemy(10*i, 10); //create enemy
            enemyStore.add(enemyObject);                                               //store in array

in paint method paint enemy

public class Display extends JPanel
    public void paintComponent(Graphics g){ 

for(int i = 0; i < enemyStore.size(); i++){      //PAINT ENEMY
    enemyObject = (Enemy)enemyStore.get(i);

in there if player dies than remove all enemies in arraylist and restart

public void mouseClicked(MouseEvent e)
    if(e.getX() > bx && e.getX() < bx + bw) {
        if(e.getY() > by && e.getY() < by + bh){
            for(int i = 0; i < enemyStore2.size(); i++){


share|improve this question

closed as too localized by Byte56, Anko, bummzack, Trevor Powell, msell May 1 '13 at 9:48

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

I think this question is actually a question about how to fix your code. I'm voting to close as too localized. – Byte56 Apr 27 '13 at 20:41
@Byte56 then it is a question for SO... – ratchet freak Apr 27 '13 at 23:51
@ratchetfreak Nope, they don't do questions that are too localized either. It is more SO oriented than gamedev oriented however. So that would be the best place for them to make the decision. – Byte56 Apr 28 '13 at 0:55

if you remove an object at index i then all subsequent objects are shifted; in your loops you always skip over the next object (you could in your actionperformed function just use enemyStore.set(i,enemyObject ) instead of removing and re-adding

in your last code block you are clearing enemyStore2 and not enemyStore (note the 2 at the end

one last note: ArrayList has a clear function which is more efficient than looping and removing and actually works properly

share|improve this answer

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