This site requires JavaScript, please enable it in your browser!
Greenfoot back

Tutorial 4: Saving the World, Making and Playing Sound

This tutorial will explain how to initialise the world with actors, as well as how to play and record sounds.

Saving the world

By now you're probably tired of always having to add new objects to the world every time we compile the code. It's possible to add some code to automatically create some worms and a crab for you -- what's more, it's possible to get Greenfoot to write that code for you! Hit Reset, and then add some worms and a crab into the world. Before you press Run, right-click on the world and select the save the world option:

World popup crabworld save the world

This adds some code to our CrabWorld class which will create the worms and crab and add them to the world at the same position next time you reset or compile. We're not going to explore the code in detail just now, but if you're curious then look in the source code for the CrabWorld class that pops up. Once you're done, you can close the window for the CrabWorld source code.

Playing and recording sounds

We can add some sound to our scenario. The scenario comes with a sound ready for you to use, named "eating.wav". We can make that sound play every time the crab eats a worm by adding a single line to our crab class that calls Greenfoot.playSound after we remove a worm from the world:

Edit crab eat narrow

Give that a try. Don't forget to turn your speakers on (or plug in your headphones). One last thing -- if you have a microphone for your computer, you can record your own sounds. In the Controls menu there is an option to show the sound recorder:

Scenario menu controls show sound recorder

Select that, and you'll get the sound recorder:

Sound recorder

Press the record button and speak (or scrunch an empty food packet, or whatever!), then press stop. You should see a green wave, and when you press play you should hear your noise played back to you. If not, there is a problem with your microphone -- try googling to get help with that. Assuming it does have some sound, you'll almost invariably have a bit of silence at the beginning and end of the sound -- you can see this in the green display, as it will have a flat horizontal line at the beginning and end before the shape:

Sound recorder need trim

Silence at the end isn't much of a problem, but silence at the beginning is irritating -- it means that when you tell the sound to play when a worm is eaten, there will appear to be a short delay before the sound starts playing, as if your game is lagging. You can clean up the silence by selecting the bit in the middle (the bit you want to keep) by clicking at the beginning (after the initial silence) and dragging to the end (before the final silence) -- the selection should be shown in grey. Then press "Trim to selection". The silence should be removed.

Save the sound by entering something in the filename box (e.g. "myeating") then pressing Save. Close the sound recorder and go back to your code. Find the line with "eating.wav" and change it to "myeating.wav" (or whatever name you used, plus the .wav extension). Then when you play your game, you should hear your own sound playing. We're close to finishing our game, but we need to add an enemy, which we will do in part 5.