Question

I have a bit of a quandary. I need to call a function inside a MovieClip once a particular sound has finished playing. The sound is played via a sound channel in an external class which I have imported. Playback is working perfectly.

Here is the relevent code from my external class, Sonus.

public var SFXPRChannel:SoundChannel = new SoundChannel;
var SFXPRfishbeg:Sound = new sfxpr_fishbeg();
var SFXPRfishmid:Sound = new sfxpr_fishmid();
var SFXPRfishend3:Sound = new sfxpr_fishend3();
var SFXPRfishend4:Sound = new sfxpr_fishend4()

public function PlayPrompt(promptname:String):void
{
    var sound:String = "SFXPR" + promptname;
    SFXPRChannel = this[sound].play();
}

This is called via an import in the document class "osr", thus I access it in my project via "osr.Sonus.---"

In my project, I have the following line of code.

osr.Sonus.SFXPRChannel.addEventListener(Event.SOUND_COMPLETE, promptIsFinished);

function prompt():void
{
    var level = osr.Gradua.Fetch("fish", "arr_con_level");
    Wait(true);
    switch(level)
    {
        case 1:
            osr.Sonus.PlayPrompt("fishbeg");
        break;
        case 2:
            osr.Sonus.PlayPrompt("fishmid");
        break;
        case 3:
            osr.Sonus.PlayPrompt("fishend3");
        break;
        case 4:
            osr.Sonus.PlayPrompt("fishend4");
        break;
    }
}

function Wait(yesno):void
{
    gui.Wait(yesno);
}

function promptIsFinished(evt:Event):void
{
    Wait(false);
}

osr.Sonus.PlayPrompt(...) and gui.Wait(...) both work perfectly, as I use them in other contexts in this part of the project without error.

Basically, after the sound finishes playing, I need Wait(false); to be called, but the event listener does not appear to be "hearing" the SOUND_COMPLETE event. Did I make a mistake somewhere?

For the record, due to my project structure, I cannot call the appropriate Wait(...) function from within Sonus.

Help?

Was it helpful?

Solution

Event.SOUND_COMPLETE is dispatched by the soundChannel object that is returned by sound.play(). This means that you must call sound.play() first and then add the listener, and you must add the listener explicitly after every time you call play.

public function PlayPrompt(promptname:String):void
{
  var sound:String = "SFXPR" + promptname;
  SFXPRChannel = this[sound].play();
  SFXPRChannel.addEventListener(Event.SOUND_COMPLETE, promptIsFinished);
}

Then in promptIsFinished you should remove the listener.

function promptIsFinished(evt:Event):void
{
    evt.currentTarget.removeEventListener(evt.type, promptIsFinished);
    Wait(false);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top