SFX at specific frames on Spritemaps


(Justin Wolf) #1

Does anyone here use any specific method for playing sound effects on specific frames of a spritemap during its animation? I know I can just check in the entity’s update loop for when that frame is playing and then play the sound effect appropriately, however, I was curious if there were any other methods to playing a sound (only once, not continuously playing) when a spritemap first reaches the frame I’d like to play a sound on.

Any thoughts?


(Bora Kasap) #2

You should use this to detect specific frame to play SFX.

var played:Boolean = false;

public function update():void
{
if (index == 5 && !played) {
play.SFXsample();
played = true; };
}

if you use any other way to do this, it doesn’t make better performance. so you don’t need to find another way…


(Bora Kasap) #3

actually that makes better performance…

if (!played) {
   if(index == 5) {
........
}
}

(Jonathan Stoler) #4

I’m no expert on optimization, but I think this is even better:

if(!played && index == 5)

This way, there’s only one if statement and it will immediately abort if the sound has been played before (more likely than index being 5, which is going to happen far more frequently).

Really, though, I don’t think you can even measure the difference, unless you have extremely specialized equipment. I also think it’s just cleaner to not have chained if statements.