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 Clips Loader

ClipsLoader stores a collection of ClipInfo objects in a HashMap, keyed by their names. The name and filename for a clip are obtained from a sounds information file, which is loaded when ClipsLoader is created. The information file is assumed to be in the subdirectory Sounds/.

ClipsLoader allows a specified clip to be played, paused, resumed, looped, and stopped. A SoundsWatcher can be attached to a clip. All this functionality is handled in the ClipInfo object for the clip.

Tip

It's possible for many clips to play simultaneously, since each ClipInfo object is responsible for playing its own clip.

The first ClipsLoader constructor loads a sounds information file, and the second initializes the HashMap of clips:

    // globals
    private HashMap clipsMap;
      /* The key is the clip 'name', the object (value)
         is a ClipInfo object /


    public ClipsLoader(String soundsFnm)
    { this();
      loadSoundsFile(soundsFnm);
    }

    public ClipsLoader()
    {  clipsMap = new HashMap();  }

loadSoundsFile() parses the information file, assuming each line contains a name and filename. For example, clipsInfo.txt used by LoadersTests is:

    // sounds
    cat cat.wav
    chicken chicken.wav
    dog dog.wav
    sheep sheep.wav

Tip

The name can be any string. The file may contain blank lines and comment lines beginning with //.

After a line's name and filename have been extracted, load() is called:

            public void load(String name, String fnm) // create a ClipInfo object for name and store it { if (clipsMap.containsKey(name)) System.out.println( ...
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