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

2018/4/14

How to make a sound plays only once even if the key is kept pressed?

TomazVDSN TomazVDSN

2018/4/14

#
if(Greenfoot.isKeyDown("e")){
           setImage("LeftUp.png");
           woo.play();

       }
Is there a way to make this sound plays only once if I keep the key is pressed?
danpost danpost

2018/4/14

#
TomazVDSN wrote...
Is there a way to make this sound plays only once if I keep the key is pressed?
Track the state of the key. The following will have the sound play each time the key is pressed:
// add field
private boolean eDown;

// action
if (eDown != Greenfoot.isKeyDown("e"))
{
    eDown = !eDown;
    if (eDown)
    {
        setImage("LeftUp.png");
        woo.play();
    }
}
If you want it only to happen on the first press of the key, please inform.
TomazVDSN TomazVDSN

2018/4/14

#
danpost, it is impressive, Thank you.
You need to login to post a reply.