Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
SoundMixer.stopAll will stop all the sound, but you have to put it in the proper place, then only it will work. Just put that SoundMixer.stopAll(); before the if condition.
And i am not seeing anywhere you are calling that destruct function
Copy link to clipboard
Copied
That doesn't change anything and why would it?
SoundMixer does stop all the sounds, the problem is that if I pause and unpause the sound inside the class and re-instanciate the class frequently (with the sound inside being paused or playing at the moment of re-instanciation), then at some point I have mupltiple copies of the sound playing.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now