Skip to Main Content
Killer Game Programming in Java
book

Killer Game Programming in Java

by Andrew Davison
May 2005
Intermediate to advanced content levelIntermediate to advanced
998 pages
26h
English
O'Reilly Media, Inc.
Content preview from Killer Game Programming in Java

The Sound of Shooting

Java 3D has three kinds of sound node classes. All three are subclasses of the Sound class.

BackgroundSound

A BackgroundSound node allows a sound to permeate the entire scene, located at no particular place.

PointSound

A PointSound node has a location, so its volume varies as the user moves (or as the sound node moves). I use PointSound nodes for the laser-beam and explosion sounds in Shooter3D.

ConeSound

A ConeSound node is a PointSound that can be aimed in a particular direction.

Before sound nodes can be added to a scene, an audio device must be created and linked to the Viewer object. This is simple if the SimpleUniverse utility class is being used (as in this example):

    AudioDevice audioDev = su.getViewer().createAudioDevice();

Tip

This line of code appears in the WrapShooter3D constructor.

SimpleUniverse was introduced in Chapter 14; it builds the view branch part of the scene graph, which specifies how the user's viewpoint is positioned in the world and includes the Viewer object.

WrapShooter3D uses initSound() to load a WAV sound file and create a PointSound object:

 private PointSound initSound(String filename) { MediaContainer soundMC = null; try { soundMC = new MediaContainer("file:sounds/" + filename); soundMC.setCacheEnable(true); // load sound into container } catch (Exception ex) { System.out.println(ex); } // create a point sound PointSound ps = new PointSound(); ps.setSchedulingBounds( bounds ); ps.setSoundData( soundMC ); ps.setInitialGain(1.0f); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Game Development with LibGDX: From Beginner to Professional

Java Game Development with LibGDX: From Beginner to Professional

Lee Stemkoski

Publisher Resources

ISBN: 0596007302Supplemental ContentErrata Page