From AS2 to AS3 – Where did it go – attachSound

Where did attachSound go in AS3?

It’s almost the same as attachMovie (I already covered attachMovie: read attachMovie post)

I was never very good in sound, and I didn’t use it a lot in my applications/animations. That changed with my new job. But for the people that remember attachSound in AS2, this is how its done in AS3…

This post is only handy when you link sound from the library, both AS2 and AS3. Dynamically loaded sound (MP3) is another story.

What has the ActionScript 2.0 Migration to say about this subject:

ActionScript 2.0  ActionScript 3.0  Comments
attachSound() Method Removed Create an instance of a Sound subclass that is associated with sound data; for example, by using new Sound() instead.

And I say again: Removed… wtf? ;)

In AS2 it will work like this:

AS2

this is code directly from the Flash Help files:

The following example attaches the sound logoff_id to my_sound. A sound in the library has the linkage identifier logoff_id.

For this example you need a sound in the library (right-click >> Linkage… >> activate ‘Export for ActionScript’) with the Indentifier ‘logoff_id‘ and this AS2 code placed in the root

example #1
[as]// #1
var my_sound:Sound = new Sound();
my_sound.attachSound(“logoff_id”);
my_sound.start();
[/as]

Oke, now in AS3:

AS3

For consistency I’m using the same names/linkage.
For this example you need a sound in the library (right-click >> Linkage… >> activate ‘Export for ActionScript’) with the Indentifier ‘logoff_id‘ and this AS2 code placed in the root

example #1
[as]// #1
var my_sound:logoff_id = new logoff_id();
var channel:SoundChannel = my_sound.play();
[/as]

I hope this will clear up some issues you have, happy coding ;)