home *** CD-ROM | disk | FTP | other *** search
- package game
- {
- import flash.events.Event;
- import flash.media.Sound;
- import flash.media.SoundChannel;
- import flash.media.SoundTransform;
- import flash.utils.clearTimeout;
- import flash.utils.getTimer;
- import flash.utils.setTimeout;
-
- public class SoundManager
- {
-
- public static const TYPE_BG:String = "c";
-
- public static var _instance:SoundManager;
-
- public static const TYPE_MAN:String = "a";
-
- public static const TYPE_BTN:String = "b";
-
- public static const TYPE_ITEM:String = "d";
-
-
- private var voiceTimer:int;
-
- private var bgStartTime:Number = -1;
-
- private var maxSound:Number = 0.75;
-
- private var nBgVolume:Number;
-
- public var soundOn:Boolean;
-
- private var nextBg:SoundChannel;
-
- private var currentSign:String;
-
- private var pBgVolume:Number;
-
- private var currentBgVolume:Number = -1;
-
- private var nextPlay:Boolean = false;
-
- private const MAX_VOLUME:Number = 0.75;
-
- public var playTable:Object;
-
- public var soundTable:Object;
-
- public function SoundManager()
- {
- maxSound = 0.75;
- bgStartTime = -1;
- currentBgVolume = -1;
- nextPlay = false;
- super();
- soundTable = new Object();
- playTable = new Object();
- soundOn = Cookie.getInstance().getValue("soundOn") == null ? true : Cookie.getInstance().getValue("soundOn");
- }
-
- public static function getInstance() : SoundManager
- {
- if(_instance == null)
- {
- _instance = new SoundManager();
- }
- return _instance;
- }
-
- private function voiceDown() : void
- {
- pBgVolume -= 0.025;
- nBgVolume += 0.025;
- if(pBgVolume <= 0)
- {
- nextBg.soundTransform = new SoundTransform(maxSound);
- playTable[TYPE_BG].soundTransform = new SoundTransform(0);
- playTable[TYPE_BG].removeEventListener(Event.SOUND_COMPLETE,onBgComplete);
- playTable[TYPE_BG] = nextBg;
- playTable[TYPE_BG].addEventListener(Event.SOUND_COMPLETE,onBgComplete,false,0,true);
- nextBg = null;
- if(currentSign == "geomBg")
- {
- voiceTimer = setTimeout(bgVoiceDown,70000,"mainBg");
- }
- return;
- }
- nextBg.soundTransform = new SoundTransform(nBgVolume);
- playTable[TYPE_BG].soundTransform = new SoundTransform(pBgVolume);
- voiceTimer = setTimeout(voiceDown,100);
- }
-
- private function onManSdComplete(param1:Event) : void
- {
- playSound("manRun",TYPE_MAN,true);
- }
-
- public function stopManSound() : void
- {
- if(playTable[TYPE_MAN])
- {
- playTable[TYPE_MAN].stop();
- try
- {
- playTable[TYPE_MAN].removeEventListener(Event.SOUND_COMPLETE,onManSdComplete);
- }
- catch(e:Error)
- {
- }
- playTable[TYPE_MAN] = null;
- }
- }
-
- private function bgVolumeDown() : void
- {
- try
- {
- pBgVolume -= 0.025;
- if(pBgVolume <= 0)
- {
- clearAllSounds();
- return;
- }
- currentBgVolume = pBgVolume;
- playTable[TYPE_BG].soundTransform = new SoundTransform(pBgVolume);
- voiceTimer = setTimeout(bgVolumeDown,100);
- }
- catch(e:Error)
- {
- clearAllSounds();
- }
- }
-
- public function loadSound(param1:String, param2:*) : void
- {
- var _loc3_:Sound = null;
- _loc3_ = new param2();
- soundTable[param1] = _loc3_;
- }
-
- public function loadSounds() : void
- {
- loadSound("mainBg",MainBg);
- loadSound("geomBg",GeometryBg);
- loadSound("getItem",GetItem);
- loadSound("pistonFall",PistonFall);
- loadSound("doorOpen",DoorOpen);
- loadSound("manClimb",ManClimb);
- loadSound("manRun",ManRun);
- loadSound("manFall",ManFall);
- loadSound("manDie",ManDie);
- loadSound("manJump",ManJump);
- loadSound("mapRotate",MapRotate);
- loadSound("splash",Splash);
- loadSound("scaleMax",ScaleMax);
- loadSound("scaleMin",ScaleMin);
- loadSound("btnDown",MenuDown);
- }
-
- public function turnSound(param1:Boolean) : void
- {
- soundOn = param1;
- Cookie.getInstance().saveValue(param1,"soundOn");
- if(param1)
- {
- maxSound = MAX_VOLUME;
- }
- else
- {
- maxSound = 0;
- }
- if(playTable[TYPE_MAN])
- {
- playTable[TYPE_MAN].soundTransform = new SoundTransform(maxSound);
- }
- if(playTable[TYPE_BTN])
- {
- playTable[TYPE_BTN].soundTransform = new SoundTransform(maxSound);
- }
- if(playTable[TYPE_BG])
- {
- playTable[TYPE_BG].soundTransform = new SoundTransform(maxSound);
- }
- if(playTable[TYPE_ITEM])
- {
- playTable[TYPE_ITEM].soundTransform = new SoundTransform(maxSound);
- }
- }
-
- public function turnOffBg() : void
- {
- try
- {
- clearTimeout(voiceTimer);
- }
- catch(e:Error)
- {
- }
- pBgVolume = currentBgVolume < 0 ? maxSound : currentBgVolume;
- voiceTimer = setTimeout(bgVolumeDown,100);
- }
-
- private function onBgComplete(param1:Event) : void
- {
- if(bgStartTime < 0)
- {
- bgStartTime = getTimer();
- }
- param1.currentTarget.removeEventListener(Event.SOUND_COMPLETE,onBgComplete);
- playTable[TYPE_BG] = null;
- if(Math.random() > 0.8 && getTimer() - bgStartTime > 80000)
- {
- nextPlay = true;
- playSound("mainBg",TYPE_BG);
- bgStartTime = getTimer() + 60000;
- }
- else
- {
- playSound("mainBg",TYPE_BG);
- }
- }
-
- private function bgVoiceDown(param1:String) : void
- {
- nextPlay = false;
- currentSign = param1;
- pBgVolume = maxSound;
- nBgVolume = 0;
- nextBg = soundTable[param1].play();
- nextBg.soundTransform = new SoundTransform(maxSound);
- voiceTimer = setTimeout(voiceDown,100);
- }
-
- public function clearAllSounds() : void
- {
- bgStartTime = -1;
- currentBgVolume = -1;
- if(playTable[TYPE_MAN])
- {
- playTable[TYPE_MAN].stop();
- try
- {
- playTable[TYPE_MAN].removeEventListener(Event.SOUND_COMPLETE,onManSdComplete);
- }
- catch(e:Error)
- {
- }
- }
- try
- {
- clearTimeout(voiceTimer);
- }
- catch(e:Error)
- {
- }
- if(nextBg)
- {
- nextBg.stop();
- nextBg = null;
- }
- if(playTable[TYPE_BTN])
- {
- playTable[TYPE_BTN].stop();
- }
- if(playTable[TYPE_BG])
- {
- playTable[TYPE_BG].stop();
- playTable[TYPE_BG].removeEventListener(Event.SOUND_COMPLETE,onBgComplete);
- }
- if(playTable[TYPE_ITEM])
- {
- playTable[TYPE_ITEM].stop();
- }
- playTable = new Object();
- }
-
- public function playSound(param1:String, param2:String = "b", param3:Boolean = false) : void
- {
- var sign:String = param1;
- var type:String = param2;
- var _repeat:Boolean = param3;
- if(soundTable[sign] == null)
- {
- return;
- }
- switch(type)
- {
- case TYPE_MAN:
- if(playTable[TYPE_MAN])
- {
- playTable[TYPE_MAN].stop();
- try
- {
- playTable[TYPE_MAN].removeEventListener(Event.SOUND_COMPLETE,onManSdComplete);
- }
- catch(e:Error)
- {
- }
- }
- playTable[TYPE_MAN] = soundTable[sign].play();
- playTable[TYPE_MAN].soundTransform = new SoundTransform(maxSound);
- if(_repeat)
- {
- playTable[TYPE_MAN].addEventListener(Event.SOUND_COMPLETE,onManSdComplete,false,0,true);
- }
- break;
- case TYPE_BTN:
- playTable[TYPE_BTN] = soundTable[sign].play();
- playTable[TYPE_BTN].soundTransform = new SoundTransform(maxSound);
- break;
- case TYPE_BG:
- if(playTable[TYPE_BG])
- {
- playTable[TYPE_BG].stop();
- playTable[TYPE_BG].removeEventListener(Event.SOUND_COMPLETE,onBgComplete);
- }
- playTable[TYPE_BG] = soundTable[sign].play();
- playTable[TYPE_BG].soundTransform = new SoundTransform(maxSound);
- if(nextPlay)
- {
- bgVoiceDown("geomBg");
- }
- playTable[TYPE_BG].addEventListener(Event.SOUND_COMPLETE,onBgComplete,false,0,true);
- break;
- case TYPE_ITEM:
- playTable[TYPE_ITEM] = soundTable[sign].play();
- playTable[TYPE_ITEM].soundTransform = new SoundTransform(maxSound);
- }
- }
- }
- }
-