Game Development Reference
In-Depth Information
ll need access to parts of the events package to be able to
dispatch events, as well as the media package, where all the
sound-related classes are stored. We
We
'
ll also need the URLRequest-
class to load external files, and the ApplicationDomain class and
getDefinitionByName method to look up sounds in the library.
Inside the SoundEngine class, we
'
'
ll add some basic properties
and the constructor for the class:
protected var _soundList:Object;
protected var _allMuted:Boolean = false;
protected var _debug:Boolean = false;
static private var _instance:SoundEngine;
public function SoundEngine(validator:SoundEngineSingleton) {
if (_instance) throw new ArgumentError ("SoundEngine is a
Singleton class. Use getInstance() to retrieve the existing
instance.");
_soundList = new Object();
}
static public function getInstance():SoundEngine {
if (!_instance) _instance = new SoundEngine(new SoundEngine-
Singleton());
return _instance;
}
The top five lines are variable declarations for the properties that
we are going to store in the engine. The _soundList property will be
used to keep track of all the SoundEngineObjects the engine creates.
The _allMuted property will help us determine if the engine is
currently muted, so that any new sounds played will be muted as
well. There is a _debug flag that can be set to toggle debug informa-
tion printing to the output window
one of the additions of my
coworkers. Finally, the _instance property is also static; it will be
used to store the one SoundEngine object that gets created, so we
can always access it.
The constructor of a Singleton should technically be private, so
that nothing outside the class can instantiate it. However, Action-
Script 3 does not support private constructors, so we have to use a
work-around, which I will discuss momentarily. First, let
s look
at the getInstance method. It is static, so it will be accessible
from anywhere as SoundEngine.getInstance(). If an instance of
the engine has not yet been created, it stores a new one in the
_instance property I mentioned earlier. It then simply returns the
instance it has created. You probably noticed that both the meth-
ods make use of a class called SoundEngineSingleton. This is an
empty class that we will define internally to prevent any other class
outside of the engine from creating a new one. Without access to
'
Search Nedrilad ::




Custom Search