Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So now we need to incorporate our sound effects into our game. So we have two MP3 files in the assets folder, explosion.mp3 and shoot.mp3. So I'm going to come into my assets class and we're going to embed those. So Embed(source="assets/explosion.mp3"), and now for class I'm going to create a private static var and call it explosionSound and it's going to be of type class.
And now I'm going to create a public static variable, which is going to be our actual sound object ourself, so public static var explosion, and this is going to be of type sound. So we're only going to create a single sound instance and we're going to use that, and that's going to help us to save on memory and performance. So I'm going to copy this embed block. So we're going to do the same thing for the shoot sound, so I'm going to change this to shoot. Change this to be shoot sound, and we're going to call our sound object shoot.
So now if we come into the init function, let's initialize those. So explosion = new explosionSound, and we're going to do the same for shoot = new shootSound. Now there is a bug in Flash Player and that when you first play a sound back there is a delay because it actually has to load it into memory. So what we're going to do in our init function is to just play it once with no volume, in that way it's actually going to load it into memory.
So we'll say explosion.play, give 0 for start time, 0 for the number of loops, and pass in a new SoundTransform object, and to that we're going to pass in a volume of 0, so that way we don't hear it, and we'll copy that, and come down, we'll paste this in and change it to shoot. And again, that's going to make sure that this is actually in memory so when we actually want to play the sound, I'm just going to do it right away.
So let's do that, let's go now to our CollisionManager class, we want to play an explosion when there is a collision between the bullets and the aliens. So what I'm going to do is to come down in this if statement and I'm going to say Assets.explosion.play, and that's going to play that explosion sound effect. Now another thing I want to do is to play a sound when I'm actually shooting a bullet, so for that I'm going to go to BulletManager, and we're going to come to our fire method and I'll just come down at the bottom here, and I'll say Assets, make sure we have that imported, .shoot.play, like that.
And now let's go ahead and test it. (sound effects) And now you can see we have those nice sound effects happening, so as I hold down the Mouse button, I'm playing that shoot sound, and we're getting those nice explosions when a bullet actually hits one of these aliens. (sound effects) So you can see incorporating sound effects is actually pretty simple, it's exactly the way you're used to doing it in other areas of Flash development.
But again, the trick is definitely to play it one time with no volume, to make sure that that sound effect is ready in memory.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97381 Viewers
61 Video lessons · 84625 Viewers
71 Video lessons · 68826 Viewers
56 Video lessons · 101292 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.