Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actionscript 3: playing sound from library with name from string

I am trying to write some actionscript 3 code to play short sounds from the library, using a dynamically created string to load it.

In AS2, I could do something like this:

mySound = new Sound();
mySound.attachSound("any concatenated string" + foo);

In AS3 however, the identifier is a class whose name, it seems, must be already known. Is there a simple way to 'attach' a sound using the identifier as a string in actionscript 3?

like image 904
Lack Avatar asked Mar 03 '26 21:03

Lack


1 Answers

First, in your library, set the class linkage of a sound file by right clicking, selecting properties and editing the Class field in the Linkage section. In this example it will be Class:FogHorn

 import flash.utils.getDefinitionByName;    
 var SoundClass:Class = getDefinitionByName("FogHorn") as Class;
 var newSound:Sound = new SoundClass(); 
 newSound.play()
like image 139
Allan Avatar answered Mar 06 '26 01:03

Allan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!