Link to home
Create AccountLog in
Avatar of allelopath
allelopath

asked on

Problem with using Thead.sleep()

I'm using AndEngine to develop for Android. Currently I have a sprite and at some point it moves off-screen and then it reappears somewhere else. I want to put a random amount of time before it reappears, so I did this:
private void addSpriteWait() {
	double nextDouble = random.nextDouble(); // between 0 and 1
        final int lowerLimit = 1000; // 1 second
        final int upperLimit = 5000; // 5 seconds
        final long waitTime = (long) (nextDouble * (upperLimit - lowerLimit) + lowerLimit);
        try {
			Thread.sleep(waitTime);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			Debug.d("addSpriteWait(): " + waitTime);
			e.printStackTrace();
		}
}

Open in new window

This does indeed wait a random amount of time, but it freezes the entire app for that time, which is not desirable. How do I wait some time before the sprite recreation but still continue with the app?
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

You'll probably have to put it on a separate thread.
Avatar of allelopath
allelopath

ASKER

new Thread().sleep(waitTime);

Open in new window

behaves the same way
All you've done there is made an anonymous thread which does the same thing as you already were doing. You need to make a class extend Thread and let it handle the sprite.
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account