0
votes

I'm having a weird problem with sounds interrupting each other in an AS3 game.

Each enemy in the game world has a movieclip containing its graphics and sounds; game code calls gotoAndPlay() to play a particular animation with corresponding sound. (the movieclips are made in Flash and the .swc is imported into a Flashdevelop project)

The problem is that when more than one instance of a particular type of enemy is onscreen at the same time, their sounds "stomp" on each other: if enemies A and B are of the same type, and A is in the middle of playing a sound, B won't be able to play that sound -- gotoAndPlay results in correct animation, but the sound doesn't play.

Similarly, if A is playing a looping sound and B plays a frame containing a stop-sound event, it will stop A's loop.

It seems pretty clear that what's happening is the Flashplayer is only allocating a single channel per movieclip type, rather than per movieclip instance, however google isn't really showing me anything.

Are there best practices for this scenario? (i.e multiple instances of the same movieclip which has sound+animation on its timeline) What am I doing wrong?!

Thanks for your time.

[edit]

To explain a bit better what I mean, let's say there's a Baddie movieclip which contains several animations, e.g the enemy running, crouching, jumping, firing a gun.

There are two layers in the movieclip, one for graphics (animation), one for sound.

On frame 20 there's a muzzleflash (on the animation layer) and a corresponding gun-shot sound (on the sound layer); on frame 40 there's a jump animation and corresponding jump sound.

My problem is: if there are two or more of these movieclips on the stage at once, and one of them is in the middle of playing the gun-shot sound, the others won't play the gun-shot sound (they'll still animate correctly though).

It seems like the Flashplayer is only allocating a single audio channel for all instances of a certain movieclip. This seems like it can't be expected behaviour since it would be useless, so I'm guessing I've messed something up with embedding (maybe there's a parameter or flag hidden somewhere which lets me say "give each instance its own sound channel"?)

1
are they on different layers? - Rachel Gallen
take a look at this managing sounds link code.google.com/p/cheezeworld/downloads/… - Rachel Gallen

1 Answers

0
votes

You should really use the Sound() and related classes, not timeline. I'm not exactly sure why this is happening, it is surprising since presumably a lot of non-coders will control sound in this fashion, but you'll have a lot more control over sound once you get used to it, and it's pretty simple. This page does a pretty good, neat job of explaining the classes you'll need:

http://www.republicofcode.com/tutorials/flash/as3sound/

I can remember having problems with lots of sounds when I started out in Flash and used the timeline, mine would always stop the previous sound before another instance of it started, once I started controlling them programmatically I never had a problem - superficially it seems easier to timeline as most users learn the playhead control functions quite early, but it can get complex and messy very quickly - far better to separate concerns and control sound with dedicated classes.

Hope this helps.