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

2012/10/24

While Loop problems with sound

drpotts2000 drpotts2000

2012/10/24

#
I am new to programming and I am currently trying to insert a while loop between 2 sounds to provide a short delay between them. I am running the crab program from the Introduction to Programming with Greenfoot. I am trying to make it where when the lobster eats the crab, it plays the "slurp" file then plays the "fanfare" file. We were instructed to use a while loop between the playing of the sound files, define an integer variable and increment it in the loop. I am supposed to make the loop terminate after 1000000000 executions. Can anybody please help. The way I have it now, the sound loops really fast and freezes up the scenario. Here is the code. import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * A lobster. Lobsters live on the beach. They like to eat crabs. (Well, in our game * they do...) * * Version: 2 * * The lobster walks around randomly. If it runs into a crab it eats it. * In this version, we have added a sound effect, and the game stops when * a lobster eats the crab. */ public class Lobster extends Animal { /** * Do whatever lobsters do. */ public void act() { turnAtEdge(); randomTurn(); move(); lookForCrab(); } /** * Check whether we are at the edge of the world. If we are, turn a bit. * If not, do nothing. */ public void turnAtEdge() { if ( atWorldEdge() ) { turn(17); } } /** * Randomly decide to turn from the current direction, or not. If we turn * turn a bit left or right by a random degree. */ public void randomTurn() { if(Greenfoot.getRandomNumber(100) > 90) { turn(Greenfoot.getRandomNumber(90)-45); } } /** * Try to pinch a crab. That is: check whether we have stumbled upon a crab. * If we have, remove the crab from the game, and stop the program running. */ public void lookForCrab() { if (canSee(Crab.class) ) { eat (Crab.class); Greenfoot.playSound ("slurp.wav"); int i = 0; while ( i < 1000000000 ) { Greenfoot.playSound ("fanfare.wav"); Greenfoot.stop(); i = i + 1; } } } }
SPower SPower

2012/10/24

#
Have you got any idea how much 1000000000 is?
Upupzealot Upupzealot

2012/10/24

#
@SPower just help him。 If he can figure things out himself, he won't come here for help. We should be patience to those who need help.
danpost danpost

2012/10/24

#
@SPower, a simple while loop can count to a billion fairly quickly (on my system, maybe 7 to 8 seconds). Of course, it makes no sense to have the Greenfoot.stop() call within the loop (it should be outside) and the same with the Greenfoot.playSound("fanfare.wav") call (that will cause the freezing, if placed within the loop as above). @drpotts2000, I believe what you are trying to do, is bide some time (with the loop) so the first sound playing can finish before playing the second sound. However, this is not a very good way of doing it, as you are tying up the CPU for the duration of the count. Anyway, move the two statements mentions in the note to SPower outside and below the 'while' block.
drpotts2000 drpotts2000

2012/10/24

#
Thank you very much. It stopped the freezing problem, but the sounds now play over each other. I know there are other ways to do this, but these are the parameters that I have been told to work with. Is there anything else that I missed. Any help is greatly appreciated. /** * Try to pinch a crab. That is: check whether we have stumbled upon a crab. * If we have, remove the crab from the game, and stop the program running. */ public void lookForCrab() { if (canSee(Crab.class) ) { eat (Crab.class); Greenfoot.playSound ("slurp.wav"); int i = 0; while ( i < 1000000000 ) { i = i + 1; } Greenfoot.playSound ("fanfare.wav"); Greenfoot.stop(); }
danpost danpost

2012/10/24

#
Unfortunately, the method you are using to 'bide the time' is dependent on the speed of the machine; so, changing the speed of the scenario will not help. The only thing I can think of is increasing the value of '1000000000' until it matches the time you need to wait.
drpotts2000 drpotts2000

2012/10/25

#
Thank you for all the help. I really appreciate it.
You need to login to post a reply.