School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,369 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,436 people online right now. Registration is fast and FREE... Join Now!




Music for Flash Game Development in AS2

 

Music for Flash Game Development in AS2, Learn how to use basic sound in AS2.

LostPunisher

1 Jul, 2009 - 05:16 PM
Post #1

D.I.C Head
Group Icon

Joined: 19 Feb, 2008
Posts: 57


Dream Kudos: 50
My Contributions
Lately I’ve been working on a flash game with a group of kids from my college. Over the summer I’ve done a bit of programming on the game, and I noticed that one of the hardest things for me to figure out was how to do music. There where a few tutorials out there, but none that really just gave me the information I wanted without throwing a bunch of extra crap at me. So today I thought I would do exactly what they didn’t. I’m going to give you the bare bones music and sound tutorial.

IPB Image

Note: I am using ActionScript 2 as this is the language that we chose to develop our game in.

So the first thing you’ll want to do is grab the music file you want to play. If you don’t have one you can download the track along with the project file for this tutorial here. The format of your audio track should be a .mp3 with a maximum bitrate of 128kbps. If you need a converter for your audio track I would suggest using Super ©. It’s super powerful, has a ton of features and best of all it’s free.To import your audio track to your flash project, simply click File -> Import -> Import to Library, browse to your audio file and then click OK. You should now see your audio file in your project library. After that, in your library, go ahead and right click on your newly added audio track and click properties.

IPB Image

This will bring up the above dialog box. Click the check box that says “Export for ActionScript” Then in the identifier box type a name that will represent this audio track. I have chosen “Disco.” This allows us to call upon our audio file whenever we choose.

IPB Image

For our next step go ahead and create a new layer by clicking on the New Layer button in the bottom left hand side of the Timeline box. Next double click on the name of the new layer and call it “Actions.” Then select the first frame on the Actions layer by left clicking once on your mouse. Finally press F9 to bring up the Actions box. Here we will write the three lines of code that will allow us play our previously added audio track.

IPB Image

Now if I scared you with the word code, don’t worry. As you can see there are only 3 lines. It’s nothing you can’t handle. Although if you’re using this for a Flash game I would expect that you already have some basic experience with ActionScript. The first line creates a new Sound object called myMusic. On the second line we then attach the audio clip with the identifier name “Disco.” After this has been done the audio track is ready to go. As a final step, myMusic.start() will cause the music to play.

That’s all there is to it! There isn’t as much flexibility is AS2 as there is in AS3 however it still works just fine for small games. Listed below are some more methods that you might find useful.

CODE

myMusic.setVolume(100); // Takes a number between 0 and 100
myMusic.stop(); // Stops the Sound Object


I’ve learnt a lot in the course of making this game, and I want to share more with everyone. If you’ve enjoyed, learnt anything, or have some suggestions for our readers please feel free to leave a few comments at Blackbunny Thanks for reading!

User is offlineProfile CardPM
+Quote Post


BetaWar

RE: Music For Flash Game Development In AS2

1 Jul, 2009 - 07:54 PM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 4,729



Thanked: 269 times
Dream Kudos: 1400
My Contributions
You should submit this to the tutorials section:
http://www.dreamincode.net/forums/showforum58.htm
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 09:03PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month