Class Variables in ActionScript3

Get Adobe Flash player

In order to play a sound, you either need to have the sound in the library, or play the sound from a URL.

In this example we will put a sound from the common libraries into the movies library, and then play it.

From the menu select Window, Common Libraries, Sounds.

open the sound library

In the sound library, select a sound. You can drag the corner of the window to make it bigger. You can also drag the little line before the word "Linkage" so that you can see the full name. After selecting a sound you can either copy and paste it into the movies library, or drag it into the movies library.

select a sound

In the library for the movie, select the sound, then right click and select properties from the list.
select the properties
<
In the sound properties window, click Advanced, then select Export for ActionScript. Change the name of the class from the long name to a valid ActionScript name such as Horn.

export for actionscript

Make a movieclip called CarHorn and name the instance (on the stage) carHorn.

Write the code as shown below:

import flash.events.MouseEvent;

carhorn.addEventListener(MouseEvent.CLICK, goHorn);
function goHorn(e:MouseEvent):void {
	var snd:Horn=new Horn();
	snd.play();
}

Download the finished movie

NEXT: URLRequest to play a sound