how to restart in java japplet?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • game2d
    New Member
    • Apr 2013
    • 59

    how to restart in java japplet?

    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
    Code:
    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
    Code:
    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
    				enemyStore.remove(i);
    				enemyObject = new Enemy(10*i, 10); //create enemy
    				enemyStore.add(enemyObject);                                               //store in array
    			}
    		}
    }

    in paint method paint enemy
    Code:
    public class Display extends JPanel
    {
        public void paintComponent(Graphics g){	
    	super.paintComponent(g);
             
    	for(int i = 0; i < enemyStore.size(); i++){      //PAINT ENEMY
    		enemyObject = (Enemy)enemyStore.get(i);
    		enemyObject.paint(g);
    	}
        }
    }

    in there if player dies than remove all enemies in arraylist and restart
    Code:
    public void mouseClicked(MouseEvent e)
    {
    if(playerObject.getDead()){
    	if(e.getX() > bx && e.getX() < bx + bw)	{
    		if(e.getY() > by && e.getY() < by + bh){
    			for(int i = 0; i < enemyStore2.size(); i++){
    				enemyStore2.remove(i);	
    			}		
    			start();
    		 }
    	 }
      }
    }
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Remove all the enemies before adding them.

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      To expand on what Rabbit said: your start() method creates new enemies every time it's called and adds them to the enemyStore collection. Now, I don't know what type of collection you're using for "enemyStore " (it's not a simple array, that's for sure - the simple array doesn't have an "add(...)" function) but every standard collection implements the Collection interface and should therefore have the functions clear(), remove(Object o) and removeAll(Colle ction<?> c). If you use one of those to remove the current enemies before adding new ones, the problem should be solved.

      Comment

      • Sherin
        New Member
        • Jan 2020
        • 77

        #4
        That's because you can't init() again after the applet has stopped. There is nothing to call the method.

        Why don't you create a reset() method that gives default values to your variables etc and makes it seem like the applet has restarted?

        Comment

        Working...