

I subtract 5 frames off the total, to make sure that the sound is finished before we need to play the impact sound.
#GREENFOOT MUSIC PLAYER HOW TO#
The length of the sound in seconds will be the time it takes to execute one frame in Greenfoot, multiplied by the number of frames before impact (“impactTime”, above - which we saw how to calculate last post).

The sound will play at 44000 samples per second, so we need to multiply the number of seconds by 44000 to get the number of samples. We can use this information to construct a sound that’s exactly as long as the time the banana will spend in the air:ĭouble lengthInSecs = timePerFrame * (impactTime - 5) We saw in our last post that for our monkeys-throwing-bananas game, we can work out how long the banana will be in flight for. Here’s an example of a sine wave on youtube.

If you treat this as a sound wave, then you get a dull tone that we recognise as an electronic beep, since it’s used so often by electronic devices. If you take an ever increasing number and pass it to the sine function, you get back this graph:
#GREENFOOT MUSIC PLAYER CODE#
Introduction 1 Chapter 1 Getting to know Greenfoot 31.1 Getting started 31.2 Objects and classes 41.3 Interacting with objects 61.4 Return types 71.5 Parameters 81.6 Greenfoot execution 91.7 A second example 101.8 Understanding the class diagram 101.9 Playing with Asteroids 121.10 Source code 131.11 Summary 15 Chapter 2 The first program: Little Crab 162.1 The Little Crab scenario 162.2 Making the crab move 182.3 Turning 192.4 Dealing with screen edges 212.5 Summary of programming techniques 25 Chapter 3 Improving the Crab-more sophisticated programming 273.1 Adding random behavior 273.2 Adding worms 303.3 Eating worms 323.4 Creating new methods 333.5 Adding a Lobster 363.6 Keyboard control 363.7 Ending the game 383.8 Adding sound 403.9 Summary of programming techniques 42 Chapter 4 Finishing the crab game 434.1 Adding objects automatically 434.2 Creating new objects 454.3 Animating images 464.4 Greenfoot images 474.5 Instance variables (fields) 484.6 Assignment 494.7 Using actor constructors 504.8 Alternating the images 524.9 The if/else statement 534.10 Counting worms 534.11 More ideas 554.12 Summary of programming techniques 56 Interlude 1 Sharing your scenarios 57I1.1 Exporting your scenario 57I1.2 Export to application 57I1.3 Export to a web page 58I1.4 Publishing on the Greenfoot Gallery 59 Chapter 5 Making music: An on-screen piano 615.1 Animating the key 625.2 Producing the sound 655.3 Abstraction: Creating multiple keys 665.4 Building the piano 685.5 Using loops: The while loop 695.6 Using arrays 725.7 Summary of programming techniques 76 Chapter 6 Interacting objects: Newton's Lab 786.1 The starting point: Newton's Lab 796.2 Helper classes: SmoothMover and Vector 806.3 The existing Body class 836.4 First extension: Creating movement 856.5 Using Java library classes 866.6 Adding gravitational force 876.7 The List type 906.8 The for-each loop 916.9 Applying gravity 936.10 Trying it out 956.11 Gravity and music 976.12 Summary of programming techniques 99 Chapter 7 Collision detection: Asteroids 1017.1 Investigation: What is there? 1027.2 Painting stars 1037.3 Turning 1067.4 Flying forward 1077.5 Colliding with asteroids 1097.6 Casting 1127.7 Adding fire power: The proton wave 1157.8 Growing the wave 1157.9 Interacting with objects in range 1197.10 Further development 1217.11 Summary of programming techniques 122 Interlude 2 The Greeps competition 123I2.1 How to get started 124I2.2 Programming your Greeps 125I2.3 Running the competition 126I2.4 Technicalities 126 Chapter 8 Creating images and sound 1278.1 Preparation 1278.2 Working with sound 1298.3 Sound recording and editing 1308.4 Sound file formats and file sizes 1318.5 Working with images 1338.6 Image files and file formats 1338.7 Drawing images 1358.8 Combining images files and dynamic drawing 1378.9 Summary 139 Chapter 9 Simulations 1419.1 Foxes and rabbits 1429.2 Ants 1459.3 Collecting food 1469.4 Setting up the world 1499.5 Adding pheromones 1499.6 Path forming 1529.7 Summary 152 Chapter 10 Additional scenario ideas 15410.1 Marbles 15410.2 Lifts 15510.3 Boids 15610.4 Circles 15710.5 Explosion 15810.6 Breakout 15910.7 Platform jumper 16010.8 Wave 16110.As well as being useful for working with triangles, the sine function can also be used for generating sounds.
