Page 1 of 1

Music for Flash Game Development in AS2 Learn how to use basic sound in AS2. Rate Topic: -----

#1 LostPunisher   User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 19-February 08

Post icon  Posted 01 July 2009 - 08:57 PM

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.

Posted 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.

Posted 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.

Posted 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.

Posted 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.

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!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1