views:

164

answers:

1
public class SndFx
{
 [Embed(source="Sounds/01.mp3")]
 public static const s01:Class;
 public static const s01s:Sound = new s01() as Sound;

 [Embed(source="Sounds/02.mp3")]
 public static const s02:Class;
 public static const s02s:Sound = new s02() as Sound;

 [etc...]
}

Can I access these attributes dynamically? I've got a number of which sound I want to play, and I want to do something like this:

SndFx["s"+numberStr+"s"].play();
+1  A: 

I don't know of a way to do that, although I'm not certain that it's actually impossible. But a way around it would be to have a singleton with a typical static method to access the instance.

SndFx.getInstance()['s'+numberStr+'s'].play();

A better approach though is to populate an array, or even a Flash Player 10 Vector, with your Sound objects, and to access the objects using that.

SndFx.soundEffectsArray[parseInt(numberStr)].play();

That allows you to check whether the parsed integer is out of bounds, et c.

richardolsson
Populating a dictionary is what I ended up doing. You can access properties dynamically with this[""].
quano