3

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?

2 Answers 2

5

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! That's a useful little function.
sure is :), also, I removed the SoundChannel import from the code as it is not needed.
0

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.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.