468,296 Members | 1,269 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,296 developers. It's quick & easy.

how to restart in java japplet?

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
3 2763
Rabbit
12,512 Expert Mod 8TB
Remove all the enemies before adding them.
Apr 27 '13 #2
Nepomuk
3,112 Expert 2GB
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
Sherin
77 64KB
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?
Feb 8 '21 #4

Post your reply

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

Similar topics

2 posts views Thread by David Eynon | last post: by
1 post views Thread by Rick | last post: by
reply views Thread by Kookymon1 | last post: by
3 posts views Thread by Sean Cleary | last post: by
5 posts views Thread by James Barrett | last post: by
3 posts views Thread by notaCons | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.