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

2013/9/16

Problems with sound

Syntac Syntac

2013/9/16

#
So I´ve the scenario with the crabs, the lobsters and the worms. What I want to do is, everytime the lobster eats a crab he should make a slurp. What´s wrong here? import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class lobster here. * * @author (your name) * @version (a version number or a date) */ public class lobster extends Animal { /** * Act - do whatever the lobster wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(); turnAtWorldEdge(); lookforcrab(); eat(); } public void lookforcrab() { if(canSee(Crab.class)) { eat(Crab.class); } } public void eat() { Actor Crab; Crab = getOneObjectAtOffset(0, 0, Crab.class); if(Crab != null) { World world; world = getWorld(); world.removeObject(Crab); Greenfoot.playSound("slurp.wav"); } } public void randomTurn() { if(Greenfoot.getRandomNumber(10) == 0) { turn(Greenfoot.getRandomNumber(90)-45); } } public void turnAtWorldEdge() { if(atWorldEdge() == true) { turn(Greenfoot.getRandomNumber(20)); move(); } else { randomTurn(); } } }
davmac davmac

2013/9/16

#
Well, what do you mean by wrong? Does the code not compile or does it run but not do what you want?
Syntac Syntac

2013/9/16

#
It runs but without the sound
davmac davmac

2013/9/16

#
Does sound work in other scenarios (eg the asteroids scenario that comes with Greenfoot)?
davmac davmac

2013/9/16

#
(or lunar lander)?
Syntac Syntac

2013/9/16

#
Yeah, in the asteroid scenario it works fine. I kinda had it so far that the lobster actually does the sound but unfortunately he does it continuously.
davmac davmac

2013/9/17

#
I don't see a problem with your code (other than the terrible indentation). Do you have a "slurp.wav" sound file in the scenario?
Syntac Syntac

2013/9/17

#
It´s all where it should be, this is driving me crazy... I created the same scenario again but it wont work.. . I´m just gonna ask my teacher, maybe it´s something realy simple and I´m just to stupid to see it. :) Thank you for your help davmac! :)
hiro6689 hiro6689

2013/9/19

#
I''m not sure where the error is... try typing it again and cheching the difference. If thet doesn't work, I'm not sure there's anything else to do.
danpost danpost

2013/9/19

#
The error is not really an error, per se. You are calling 'lookforcrab' which will eat the worm without sound; then, you are calling 'eat' which would (if a crab was left to eat) make a sound when eating. Just remove the 'eat' method (and the call to it from the 'act' method) and add the command to play the slurp in the 'lookforcrab' method.
davmac davmac

2013/9/19

#
Well spotted, danpost!
You need to login to post a reply.