Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to stop / destroy a sound object?

Guest
Dec 02, 2013 Dec 02, 2013

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!

TOPICS
ActionScript
501
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 02, 2013 Dec 02, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 02, 2013 Dec 02, 2013
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines