Game Development Reference
In-Depth Information
case JUMP_FALLING: // Falling down after jump
if (jumpKeyPressed && hasFeatherPowerup) {
AudioManager.instance.play(
Assets.instance.sounds.jumpWithFeather, 1,
MathUtils.random(1.0f, 1.1f));
timeJumping = JUMP_TIME_OFFSET_FLYING;
jumpState = JUMP_STATE.JUMP_RISING;
}
break;
}
}
The changes in the code for BunnyHead trigger the sound effects for the jumped and
jumped-in-mid-air events at the right time. The jumpWithFeather sound is played
using a different play() method of the AudioManager class. It is also provided with
a random pitch value in the range from 1.0 to 1.1, which adds a little change in the
frequency, rendering the rapidly repeated sound effect more interesting.
These were the last changes and thus we conclude this chapter about working with
audio in LibGDX and how to use it in Canyon Bunny.
Currently, sound pitching does not work in the GWT backend
that is used in LibGDX to support the WebGL target platform. The
sound will just play at its normal rate if pitching is used.
Summary
In this chapter, you learned how to use LibGDX's four audio interfaces, Sound ,
Music , AudioDevice , and AudioRecorder , and about their specific use cases. We
took a look at the freely available and open source sound generators and used one
of them to create sound effects that can be used in Canyon Bunny. We also added
background music to the game and understood that LibGDX takes care of when a
music instance being played needs to be paused and resumed in terms of the life
cycle in a LibGDX application. A manager class called AudioManager was created
to gain centralized control over any audio-related actions. Finally, playing music
and sounds turned out to be a very straightforward process in conjunction with the
use of our audio manager. We had to add just a few new lines of code to trigger the
music and sound effects at the right time for each event.
In the next chapter, we will look at some advanced programming techniques. This
includes using a physics engine suitable for two-dimensional game objects, creating
a shader program to apply a simple monochrome filter effect, and querying the
accelerometer hardware to implement an alternative input method.
 
Search WWH ::




Custom Search