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

2020/1/10

sound won't play and gives runtime error

LightningFast LightningFast

2020/1/10

#
hi I'm trying to play a "boo" sound and I've saved it in the sound folder in the desktop. everything compiles but gives a runtime error that it can't find the sound! HELP
danpost danpost

2020/1/10

#
LightningFast wrote...
hi I'm trying to play a "boo" sound and I've saved it in the sound folder in the desktop. everything compiles but gives a runtime error that it can't find the sound! HELP
Code?
LightningFast LightningFast

2020/1/10

#
nevermind, I literally JUST figured it out haha!! but I do have another question of how to make the sound only play once when the spacebar is hit?
danpost danpost

2020/1/10

#
LightningFast wrote...
nevermind, I literally JUST figured it out haha!! but I do have another question of how to make the sound only play once when the spacebar is hit?
Attempted code?
LightningFast LightningFast

2020/1/10

#
here is the code if you need it import greenfoot.*; public class Hero extends Actor { public void act() { checkKeys(); } public void checkKeys() { if (Greenfoot.isKeyDown("space")) Greenfoot.playSound("Boo.wav"); } }
LightningFast LightningFast

2020/1/10

#
I didn't attempt it yet but I was looking at the API page at it was like it'll play in a sequence (got confused)
danpost danpost

2020/1/10

#
LightningFast wrote...
here is the code if you need it << Code Omitted >>
You can probably get away with creating one instance of the sound (your current code will create multiple instances, one after another, for each act step that the space key is down):
import greenfoot.*; 

public class Hero extends Actor
{
    GreenfootSound booSound = new GreenfootSound("Boo.wav");
    
    public void act()
    {
        checkKeys();
    }    
    
    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("space")) booSound.play();
    }
}
It will repeat if the space key is held down, but it will not play on top of itself.
LightningFast LightningFast

2020/1/10

#
oh I see, thank you!
You need to login to post a reply.