views:

31

answers:

2

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?

A: 

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.

alxx
Perhaps my question wasn't clear enough - I know how to use sounds as described in that link, but that only covers sounds the class of which you know at compile time.Can I load a sound from the library with an identifier which is created in a concatenated string at runtime?
Ottawa
A: 

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()
Allan
Thank you! That's a useful little function.
Ottawa
sure is :), also, I removed the SoundChannel import from the code as it is not needed.
Allan