How to stop / destroy a sound object?
I have a function that instanciates a MovieClip class:
public function loadPart(spec:int) {
if(part != null) {
part.destruct();
removeChild(part);
}
pClass = getDefinitionByName('part'+spec) as Class;
part = new pClass();
addChildAt(part, 0);
}
The class code in general looks like this:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.getTimer;
import flash.media.SoundChannel;
import flash.media.Sound;
import flash.media.SoundMixer;
public class part0 extends MovieClip {
......
public var snd:Sound;
public var sch:SoundChannel = null;
public function part0() {
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event) {
removeEventListener(Event.ADDED_TO_STAGE, init);
.......
// sound
snd = new part_0();
sch = snd.play(1000);
}
private function soundSwitch(_mod:int = -1) {
if(_mod) {
sch = snd.play(pos);
} else {
pos = sch.position;
sch.stop();
}
}
public function destruct() {
if(hasEventListener(Event.ENTER_FRAME)) {
removeEventListener(Event.ENTER_FRAME, onEF);
}
// sound
SoundMixer.stopAll();
}
}
}
If I spam the funcfion that instanciates this class, sooner or later I have multiple copies of the same sound playing.
Can anyone please tell me why it might be happening and how to fix it?
Thank you!
