Game Development Reference
In-Depth Information
How to do it...
First of all, we need some basics set up.
1. We create a new application extending
SimpleApplication
, and add an
Au-
dioNode
field called
audioNode
.
2. In addition to this, we need to keep track of passed time with a
float
field called
time
and another
float
field called
pauseTime
, which we set to
0.7f
.
3. In the
simpleInitApp
method, we create a new
audioNode
instance:
new AudioNode(assetManager, "Sound/Effects/Foot
steps.ogg");
4. We override the
simpleUpdate
method and begin by checking whether
time
is
more than
pauseTime
.
5. If it is, we should set a new float called
pitch
. This should have a value of
1f
+- 10%
, which can be achieved with the following code:
FastMath.nextRandomFloat() * 0.2f + 0.9f.
6. Then, we call
audioNode.setPitch(pitch)
to set it.
7. Since this particular sound file plays four footsteps in sequence, we tell the node to
not start from the beginning and only play the last of the footsteps by skipping for-
ward in time, using the following code:
audioNode.setTimeOffset(2.0f);
audioNode.playInstance();
8. Before exiting the
if
statement, we set
time
to
0
.
9. Finally, we shouldn't forget to increase
time
by tpf.
10. Try running the application now. We should hear the same sound over and over
again but with a slight variation.
11. We can use
LowPassFilter
to further vary the sound. We instantiate it by sup-
plying a float for the general volume, and another for the high frequency volume.
To get the most variation, we can supply two random values between 0f and 1f:
LowPassFilter filter = new
LowPassFilter(FastMath.nextRandomFloat(),
FastMath.nextRandomFloat());