Game Development Reference
In-Depth Information
// Right
findViewById(R.id.btn_right).setOnTouchListener(
new View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent evt) {
int action = evt.getAction();
if ( action == MotionEvent.ACTION_DOWN) {
Natives.sendNativeKeyEvent(Natives.EV_KEYDOWN
, DoomTools.KEY_RIGHTARROW);
}
else if ( action == MotionEvent.ACTION_UP) {
Natives.sendNativeKeyEvent(Natives.EV_KEYUP
, DoomTools.KEY_RIGHTARROW);
}
return true;
}
});
// More ...
}
Handling Audio Independently of the Format
The audio classes are implemented in the package
doom.audio
and consist of two files:
AudioManager
and
AudioClip
.
AudioManager
is a singleton class similar to the
AudioManager
class presented in the previous
chapter. Some of the method signatures are different to accommodate the Doom engine,
such as the following:
preloadSounds()
: This method preloads the most common Doom
sounds to improve performance. Sounds are encoded in WAVE format.
startSound(String name, int vol)
: This method starts the sound
given by a name key at volume
vol
. The key does not include the file
extension, and the volume ranges from 0 to 100.
startMusic (Context ctx, String key, int loop)
: This method
starts a background music file given by
key
and loops if
loop
is set to
anything other than 0. An Android context is required by the background
AudioClip
.
stopMusic (String key)
: This method stops the background music
given by
key
.
setMusicVolume (int vol)
: This method sets the background music
volume.
vol
ranges from 0 to 100.
A great thing about
AudioClip
is that it provides a format-independent way of playing
sound (behind the scenes Android will take care of the dirty work of dealing with the format
drivers), plus it will work in all versions of Android, thus giving you the widest range of
device support.