I want to wait a moment (.5 seconds) before I increase the sound, how can i achieve this without using Thread.sleep, because i want my game to keep runnig.
-
1\$\begingroup\$ When you say "increase sound" do you mean increasing the volume? This seems like a game logic problem: you just have some counter somewhere that ticks off the time then does what you want it to do. \$\endgroup\$– TetradMar 5, 2012 at 7:20
1 Answer
You can either use a java timer approach: http://developer.android.com/reference/java/util/Timer.html
Or you could simply keep track of the time elapsed since the increase volume action began. Maybe something like this in your render method or somewhere else:
float elapsedTime;
long actionBeginTime;
boolean volumeIncreasing=false;
public void render () {
if(volumeIncreasing){
elapsedTime=(System.nanoTime()-actionBeginTime)/1000000000.0f;
if(elapsedTime>0.5f){
increaseVolume(); //your method that increases volume
volumeIncreasing=false; //can be set here or in increaseVolume()
}
}
}
Of course you need to set volumeIncreasing to true and actionBeginTime=System.nanoTime() somewhere when the action begins.
-
3\$\begingroup\$ Only to add that LibGDX has its own delta value implementation (it uses System.nanoTime() internally) and you get it by calling Gdx.graphics.getDeltaTime(). That way you don't have to calculate by hand the elapsedTime. \$\endgroup\$– arielsanMar 6, 2012 at 12:28