473,327 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 3139
Rabbit
12,516 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

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

Similar topics

2
by: David Eynon | last post by:
I know that the basic methods that can be overwritten for class JApplet are init, and paint. However, the API does not specifically state this, and JApplet does not describe this right away.. How...
1
by: Rick | last post by:
Hi, I'm having problems with Java applets. I just generated a JApplet (also tried normal Applet) in Netbeans which is delivered with the newest JDK. When I compile it in Netbeans it seems to...
0
by: Kookymon1 | last post by:
This is an attempt to respond to an older question (several months). Date: 2002-03-07 13:10:23 PST Subject: On the Common DOM API and Applets. The original message was: >LiveConnect and the...
3
by: Sean Cleary | last post by:
In Java How to program, 5th edition, I can not get past the example in chapter 3, page 89 to page 96. Is there some errata for this? I did not/could not load all the tools. I am coming from Slick...
7
by: sreenulanka | last post by:
please help me in my forum i have labels and textareas .iwant to add textarea components dyanamically when i am clicking the button.please help me import java.awt.*; import javax.swing.*;...
5
by: James Barrett | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi all, I am experimenting with JApplet and JPanel. My JApplet contains a JPanel called jpanel1 size 210x210. I created a class myPanel which...
3
by: olafatia | last post by:
import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; import java.io.File; import java.util.*; /* <Applet Code="MyPaint.class" width=400 height=400>...
3
by: notaCons | last post by:
hello im quite newbies in Java Programing so any one can help me. i will be plz.... what am i trying here is try convert The JFrame to JApplet, but when i change the JFrame to JApplet 4 Error come...
3
by: gerrybytes | last post by:
Hi all, I created a Java application which allows me to send information to my mysql database using the jdbc connector via a JFrame. However when i modified the file to be a JApplet the info will...
2
by: danielmessick | last post by:
In the Java applet below, i'm trying to make the text box change color when the slider is moved to the left and right. Right now when I move the JSlider left or right the text in the text box changes...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.