Copy link to clipboard
Copied
Hello,
How do I play a chain of events, sounds at the end of sounds?
Is there someway to pass a variable to Event.SOUND_COMPLETE?
If I could pass an int along side it, I could know which sound to play next afterwards?
I'm working on a nice game about to be published. We have voice after voice playing, but I haven't found a way to do it properly so you can do two different sounds with events at the end of each of them. I can only do one sound with an event at the end of it. If I try another at the same time, unexpected things happen.
The following code simply plays a sound, then calls clearpingmainapplication
var sc:SoundChannel = Sound(sounds[soundName]).play();
sc.soundTransform = soundTransform;
sc.addEventListener(Event.SOUND_COMPLETE, clearpingmainapplication);
Then this sends an event to the main application to know to do the next thing after the sound has played.
private function clearpingmainapplication(e:Event):void
{
SoundChannel(e.target).removeEventListener(Event.SOUND_COMPLETE, clearpingmainapplication);
trace("soundmanager: call main stage");
superstage.dispatchEvent(new InGameEvent(InGameEvent.SOUND_EVENT_FINISHED,new Point(1,1)));
}
My code works, but you can only have one sound with event afterwards at a time. If you try to send two different actors speaking at the same time, I only get one generic event with no data to signifiy the end, so theres no way to know which one to advance. For this complicated sounds playing after sounds to work, or events happening after sounds, I need to be able to pass a variable along with Event.SOUND_COMPLETE
Copy link to clipboard
Copied
I'm using:
sc.addEventListener(Event.SOUND_COMPLETE, clearpingmainapplication);
I'd like to be able to do:
var x:int;
x=someidentifier;
sc.addEventListener(Event.SOUND_COMPLETE, clearpingmainapplication(x));
or
sc.addEventListener(Event.SOUND_COMPLETE, clearpingmainapplication,x);
so I could be able to do:
private function clearpingmainapplication(e:Event,x):void
{
SoundChannel(e.target).removeEventListener(Event.SOUND_COMPLETE, clearpingmainapplication);
trace("soundmanager: call main stage");
superstage.dispatchEvent(new InGameEvent(InGameEvent.SOUND_EVENT_FINISHED,new int(x));//I could send an event with an int to the main program
}
Copy link to clipboard
Copied
I tried making a new class:
SoundChannelplus extends SoundChannel{
public int whicheventgetssent;
}
But it said SoundChannel was final, so I couldn't do it.
Is it a legal move of me to edit the core files of AS3 to get rid of the final tag?
Or is there some other way to do this?
Copy link to clipboard
Copied
I found the solution, but it is ugly.
if(eventnum==1)
sc.addEventListener(Event.SOUND_COMPLETE, clearpingmainapplication1);
if(eventnum==2)
sc.addEventListener(Event.SOUND_COMPLETE, clearpingmainapplication2);
if(eventnum==3)
sc.addEventListener(Event.SOUND_COMPLETE, clearpingmainapplication3);
etc
private function clearpingmainapplication1(e:Event):void
{
SoundChannel(e.target).removeEventListener(Event.SOUND_COMPLETE, clearpingmainapplication1);
superstage.dispatchEvent(new InGameEvent(InGameEvent.SOUND_EVENT_FINISHED,new int(1)));//this code may be wrong
}
private function clearpingmainapplication2(e:Event):void
{
SoundChannel(e.target).removeEventListener(Event.SOUND_COMPLETE, clearpingmainapplication2);
superstage.dispatchEvent(new InGameEvent(InGameEvent.SOUND_EVENT_FINISHED,new int(2)));//this code may be wrong
}
private function clearpingmainapplication3(e:Event):void
{
SoundChannel(e.target).removeEventListener(Event.SOUND_COMPLETE, clearpingmainapplication3);
superstage.dispatchEvent(new InGameEvent(InGameEvent.SOUND_EVENT_FINISHED,new int(3)));//this code may be wrong
}
For every unique event I want to play after a sound, I need to make a new function. It is ugly, but at least it works. This whole ordeal could be fixed if the datatype SoundChannel had an unused integer, or if it wasn't cast as final so I could add an integer to it.
I doubt anyone else is doing simultaneous sounds chaining with other sounds, or other people would be having this problem too.
Copy link to clipboard
Copied
This doesn't sound like a deficiency in the AS3 core datatypes as such, more something that should be easily fixed by re-architecting your app a bit.
Could you try encapsulating the code for playing a chain of sounds into it's own custom class? i.e. "SoundChain" or something similar?