Green_qaue
Green_qaue

Reputation: 3661

Wait for object to finish moving, then do action

I'm wondering if there is any built in way in Java to wait until something is finished, then keep going in the code.

In my case, I move an object from x to destinationX by 50px per frame when I press the object. To wait for that movement to finish before I keep going in my code I use a Timer atm, but I'm worried that might not be the best solution if someone runs on low fps.

Is there any way to do this without using a bunch of flags or a Timer? I looked into Events but can't find decent instructions on how I could apply it in my case.

Upvotes: 0

Views: 106

Answers (2)

dimplex
dimplex

Reputation: 494

You can perform your actions in different threads and use Thread.join() as described here.

Upvotes: 0

colouredmirrorball
colouredmirrorball

Reputation: 331

You could work with states (= an integer) and change states once a condition is met, like

if(state == 1) moveObject();
if(state == 2) keepGoing();

and you change state from 1 to 2 once you reached the destination in moveObject();.

Upvotes: 1

Related Questions