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

2013/2/26

Greenfoot.playSound plays continuously

robert_k robert_k

2013/2/26

#
Hi I'm just starting out with the crab project. All is fine, except when I add the eating.wav file (which is supposed to play when the crab goes over a worm) it simply plays over and over from execution of the program. My code is below and runs fine, except for the continuous sound.
robert_k robert_k

2013/2/26

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Crab extends Actor
{
    /**
     * 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() 
    {
        moveAndTurn();
        eat();
    }    
    public void moveAndTurn()
    {
        move(4);
        
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-3);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            turn(3);
        }
    }  
    
    public void eat()
    {
        Actor worm;
        worm = getOneObjectAtOffset(0, 0, Worm.class);
        if (worm != null);
        {
            World world;
            world = getWorld();
            world.removeObject(worm);
            Greenfoot.playSound("eating.wav");
        }
    }    
}
davmac davmac

2013/2/26

#
Your code for Crab is fine. I think one of your other actor classes must be responsible.
robert_k robert_k

2013/2/26

#
Everything is as in the tutorial. I have not changed any other files. I think it must be something to do with my system but I'm new and I don't know how my system can be affected by Java, or Greenfoot etc. There is a post from someone else with this problem but it didn't get answered, so I'm sure I'm not the only one who's had this problem. I mean it's not the end of the world, but I'd like to iron out problems as I go along if possible. Any help appreciated.
robert_k robert_k

2013/2/26

#
The sound plays at every act, and when the crab is eaten by the new lobster it stops playing.
robert_k robert_k

2013/2/26

#
funny though, it doesn't happen with lunar lander, which appears to use the sound file in the same way.
davmac davmac

2013/2/26

#
Could you upload your scenario here (with source code)?
robert_k robert_k

2013/2/27

#
Hi davmac, thanks
robert_k robert_k

2013/2/27

#
davmac davmac

2013/2/27

#
The problem is in your Lobster class:
    public void eat()
    {
        Actor crab;
        crab = getOneObjectAtOffset(0, 0, Crab.class);
        if (crab != null);
        {
            World world;
            world = getWorld();
            world.removeObject(crab);
            Greenfoot.playSound("eating.wav");
            
        }
    }
There is a semi-colon after 'if (crab != null)' which shouldn't be there, meaning that the code below it always gets executed.
robert_k robert_k

2013/2/27

#
Thanks davmac, much obliged to you.
You need to login to post a reply.