0

I have a basic Java problem in my app.
I'm creating a program of bouncing balls:

  1. I have 10 Image Views of 10 different balls.
  2. I have a Ball class which uses its own thread (ball's physics).
  3. In MainActivity I create a new ball (first Image view).

    balls[0] = new Ball (this,images[0])

  4. ? ? ? Everything is perfect until step 4, I can see a bouncing ball on my screen, but now I want to add the second ball and so on. I want to wait 5 seconds until my next ball appears and here I'm stuck and have thread issues.

My question is : where should I place the next command balls[1] = new Ball (this,images[1]); and how can I suspend it to wait 5 seconds before it starts.

Should I create another thread in main activity and use "sleep"? and if so- it will be a thread calling a new thread (my problem)

SHAI
  • 789
  • 3
  • 10
  • 43

1 Answers1

1

Don't use sleep as you don't want to put your Activity on hold and make it unresponsive.

If you want to wait 5 seconds, a Handler might help you, using postDelay:

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                   balls[1] = new Ball (this,images[1]); 
            }
        }, 5000); //in milliseconds - the code inside run() will run after 5 seconds

You can offcourse create a loop and use it to handle all balls.

SuperFrog
  • 7,631
  • 9
  • 51
  • 81