Main Content

You are here:

Basic Sound in AS3

Welcome to a fairly quick(ish) free ActionScript tutorial, about how to rig up basic sounds in AS3. When I say basic, I mean the sort of sounds that happen when you do something - a bang, a crash, a shout of 'Goal!!' or whatever. One off little things to let you know you've done something. The limitations of today's tutorial (which features only five lines of code, two of which are imports) will be that we will be focusing on loading an mp3, in the same folder as your swf and that might sound a little odd if attempted to play more than once at the same time.

I should mention now that this is in response to an email I had a couple of weeks ago from someone called Joe. He asked:

I would like a sound to be played when the 'player' hits the 'target' and/or 'enemy' ... Thanks For The Help, Joe

So I thought I'd write a tutorial about this. This will be perfectly suited to most games, including, as in Joe's case, my Avoider tutorials. Anyhow, let's get started. Imagine you've got a game or other application ready, and it's all set up. Or complete an early tutorial, it's your choice. Put your sound, in the same folder as where you're .swf will be. Now, add these extra imports:


As you can probably guess, is the basic sound class, used for creating �Sound' objects., on the other hand, handles loading the sound from an external file. Why not have it in your library? Well, I suppose there's no good reason to have it in there anyway. With the flexibility of code, you can load in the sound at a point you choose, removing the need for unnecessary items to be loaded, and items to be loaded at the same time (at the beginning). The point you choose, which could be near the start of the movie, or even in the document class constructor, should come a fair bit before any sound needs to be played.

Load it right at the start: Your first step will be to add public var sound:Sound = new Sound(new URLRequest("scored.mp3")); amongst all your other declarations, just inside your document class. This declares a new variable, and immediately assigns it its file, so it immediately starts loading the sound. Put the relative path to the sound file in between the quote marks, which gets passed to an URLRequest (a file-getter), which in turn gets passed to the Sound() constructor and declared as a sound. Easy really.

Loading it at some other point: Same as above, but you'd be better off splitting the line into two parts. The first part is just this line, as a variable declaration, with all the others, just inside the document class: public var sound:Sound;

Now, where you want it to load, assign the sound to this variable, with the line : sound = new Sound(new URLRequest("scored.mp3"));

When you want it to play, regardless of the above, just type; . It is literally as easy as that, and hence my tutorial is now at an end � you should now be able to quickly add these �basic' sounds to your Flash/ActionScript constructions. See you again for another free ActionScript tutorial,