Actionscript 3: playing sound from library with name from string

10,404

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()
Share:
10,404
Lack
Author by

Lack

Updated on June 04, 2022

Comments

  • Lack
    Lack almost 2 years

    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?