Wednesday, October 5, 2016

java - Waiting specific time to increase sound libgdx


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.




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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...