I'm working on a piano exercise in which when you press a key in the keyboard the sound only play's once and not continuously...below is the code. However I'm having trouble understanding how the private boolean is down works in all of this within the loops. I know it prevents from having the sound play continuously if the g key is kept pressed but I need someone to help me understand the "Why or How" it works, please?
public void act()
{
if ( !isDown && Greenfoot.isKeyDown("g"))
{
isDown = true; // just played a note
setImage("white-key-down.png");
Greenfoot.playSound("3a.wav");
}
if ( isDown && !Greenfoot.isKeyDown("g"))
{
isDown = false;
setImage("white-key.png");
}
/****
if (Greenfoot.isKeyDown("g"))
{
setImage("white-key-down.png");
Greenfoot.playSound("3a.wav");
}
else
{
setImage("white-key.png"); ****/
}

