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

2017/3/8

make sound when turning a certain amount?

Mjosue Mjosue

2017/3/8

#
Play a sound whenever the crab turns more than 18 degrees. I know how to add a sound, but how do I make the sound from a turn? I can not recall.
Mjosue Mjosue

2017/3/8

#
public class Crab extends Actor { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; /** * Create a crab and initialize its two images. */ public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); wormsEaten = 0; } /** * Act - do whatever the crab wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeypress(); move(5); lookForWorm(); switchImage(); moveAround(); } /** * Alternate the crab's image between image1 and image2. */ public void switchImage() { if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } /** * Check whether a control key on the keyboard has been pressed. * If it has, react accordingly. */ public void checkKeypress() { if (Greenfoot.isKeyDown("left")) { turn(-4); } if (Greenfoot.isKeyDown("right")) { turn(4); } } /** * Check whether we have stumbled upon a worm. * If we have, eat it. If not, do nothing. If we have * eaten eight worms, we win. */ public void lookForWorm() { if ( isTouching(Worm.class) ) { removeTouching(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten = wormsEaten + 1; if (wormsEaten == 8) { Greenfoot.playSound("fanfare.wav"); Greenfoot.stop(); } } } public void moveAround() { move(4); turn(Greenfoot.getRandomNumber(91)-45); } } THIS IS WHAT I HAVE SO FAR
Super_Hippo Super_Hippo

2017/3/8

#
You have:
turn(Greenfoot.getRandomNumber(91)-45);
Try this:
int turningAmount = Greenfoot.getRandomNumber(91)-45;
turn(turnAmount);
if (turnAmount>18 || turnAmount<-18)
{
    //play a sound
}
I have the feeling that you will hear that sound quite often though.
Mjosue Mjosue

2017/3/8

#
The professor wanta the sound to play whenever the crab turna 18degrees or more.
Nosson1459 Nosson1459

2017/3/9

#
Mjosue wrote...
The professor wanta the sound to play whenever the crab turna 18degrees or more.
So what's your problem? The code given by Hippo will find the amount of degrees to turn and if it's greater than 18 or less than -18 (for turning in the other direction) it will play a sound.
You need to login to post a reply.