ok so i kind of know how to restart but the problem is that when ever i restart my animation get faster and faster.

so i have main.java and alot of other objects. so one class for player one for player one for background etc...

public class main exteds JApplet actionlistener
{
  public void init()
  {
    //create window stuff
  }

  public void start()
  {
    player_class = new player1();
    //create all classes

    timer_class = new Timer(10, this);
    timer_class.start();
  }

  public void actionPerformed(ActionEvent e)
  {
    //all collsion stuff go in here
      repaint();
  }

  public void paint(graphics g)
  {
   //paint here
  }

  public void stop(){} //i dont use this method. well i dont know how to

  public void mousePressed(MouseEvent e)
  {
  //restart button here
    if(...)
    {
      if(...)
      {
       //if user hit restart button
       //i am calling start() method again so it restart
       start();   //--------------this is how i am restarting
      }
    }
  }


}

i think what is happing is that when i call start(); method at bottom it double my timer.
so 1st my timer is 10 than restart now its 20 ... and so on. that why my animation get faster and faster?

any idea how can i fix this issue. or is there a better logic to restart?

Hi, hwoarang69.
I'm not able to test your code right now, but I'll take a shot to guess, where's the problem sits. It can be in this method:

public void actionPerformed(ActionEvent e)
{
    //all collsion stuff go in here
    repaint();
}

just check, how many times it been called every time when you perform an action.

Take a look a these few links .. I guess, there're some good info and examples:
Java applets
CA597 Tutorial - Simple Applets
javax.swing.Timer

It looks like maybe you start a new timer when you restart, but the old timer is still running, so now your actionPerformed gets called twice every 10 mSec, and every time you restart that gets worse. Make sure that when you restart either you just keep using the old timer, or you stop the old timer before starting a new one.

  • never to paint to the Top-Level Containers, use JPanel instead

  • prepare all Objects to the Array (if there are more than one element)

  • use paintComponent inastead of paint

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.