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.
开发者_JAVA技巧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?
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()
http://www.adobe.com/livedocs/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000287.html
This is for CS3. If your environment differs, search section "Embedding sounds" in help.
精彩评论