0

I'm creating a racing game and in my game I have 2 frames. One is the start screen and the second is where the race will take place. I have a button that takes me to my second frame. However the problem I am running into is that when I start the program the racing cood will start executing. my current solution is to have another button on the second frame to start the code, but when i do that the cars will teleport to the finish line without slowing down.

This is my code to fun the cars

play.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
    int r1 = 0;
    int r2 = 0;
   do {
       //car random num
         r1 = r1 + (int) (Math.random() * 10);
         r2 = r2 + (int) (Math.random() * 10);
         r3 = r3 + (int) (Math.random() * 10);
         r4 = r4 + (int) (Math.random() * 10);
         r5 = r5 + (int) (Math.random() * 10);
   //car1
   car1.setBounds((r1), (115), car1.getPreferredSize().width, car1.getPreferredSize().height);
   //car2
   car2.setBounds((r2), (165), car2.getPreferredSize().width, car2.getPreferredSize().height);

});

I have tried to use Thread.Sleep(_;but it doesn't work.

1 Answers1

-1

Try to put Thread.sleep() before your actionPerformed method and inside a try / catch, like this:

try {
   Thread.sleep(2000);
} catch (InterruptedException e) {
   e.printStackTrace();
}

And your do / while doesn't have while condition?

Juliano Pacheco
  • 521
  • 1
  • 5
  • 13