By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,653 Members | 1,419 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,653 IT Pros & Developers. It's quick & easy.

how to restart in java japplet?

P: 59
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
Expand|Select|Wrap|Line Numbers
  1. public void start()
  2. {
  3. for(int i = 0; i < 10; i++){  
  4.     enemyObject = new Enemy(10*i,10);
  5.     enemyStore.add(enemyObject);    //store in array
  6. }
  7. }
in here i am making enemy move, collision etc and if enemy is dead than i am remooving it from arraylist
Expand|Select|Wrap|Line Numbers
  1. public void actionPerformed(ActionEvent e)
  2. {
  3.     for(int i = 0; i < enemyStore.size(); i++){       
  4.         enemyObject = (Enemy)enemyStore.get(i);
  5.             if(!enemyObject.getDead()){               //if enemy is not died
  6.                 //make enemy move, collision etc... here
  7.                                 ....
  8.             }
  9.             else{           //remove enemy and create another one
  10.                 enemyStore.remove(i);
  11.                 enemyObject = new Enemy(10*i, 10); //create enemy
  12.                 enemyStore.add(enemyObject);                                               //store in array
  13.             }
  14.         }
  15. }

in paint method paint enemy
Expand|Select|Wrap|Line Numbers
  1. public class Display extends JPanel
  2. {
  3.     public void paintComponent(Graphics g){    
  4.     super.paintComponent(g);
  5.  
  6.     for(int i = 0; i < enemyStore.size(); i++){      //PAINT ENEMY
  7.         enemyObject = (Enemy)enemyStore.get(i);
  8.         enemyObject.paint(g);
  9.     }
  10.     }
  11. }

in there if player dies than remove all enemies in arraylist and restart
Expand|Select|Wrap|Line Numbers
  1. public void mouseClicked(MouseEvent e)
  2. {
  3. if(playerObject.getDead()){
  4.     if(e.getX() > bx && e.getX() < bx + bw)    {
  5.         if(e.getY() > by && e.getY() < by + bh){
  6.             for(int i = 0; i < enemyStore2.size(); i++){
  7.                 enemyStore2.remove(i);    
  8.             }        
  9.             start();
  10.          }
  11.      }
  12.   }
  13. }
Apr 27 '13 #1
Share this Question
Share on Google+
2 Replies


Rabbit
Expert Mod 10K+
P: 12,421
Remove all the enemies before adding them.
Apr 27 '13 #2

Nepomuk
Expert 2.5K+
P: 3,112
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(Collection<?> c). If you use one of those to remove the current enemies before adding new ones, the problem should be solved.
Apr 29 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.