Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to restart in java japplet?

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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
    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
    				enemyStore.remove(i);
    				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){	
    	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
    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();
    		 }
    	 }
      }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to restart in java japplet?

    clear the enemyStore when the player dies

Similar Threads

  1. how to restart applet without closing it
    By hwoarang69 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 14th, 2013, 07:03 PM
  2. how to restart my program
    By jack_nutt in forum Java Theory & Questions
    Replies: 15
    Last Post: November 16th, 2011, 07:39 PM
  3. Sliding puzzle Restart button(same exact game)
    By carterb32 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 21st, 2011, 04:29 AM
  4. initializing a java JApplet
    By j_a_lyons in forum Java Theory & Questions
    Replies: 0
    Last Post: January 8th, 2011, 04:49 PM