I'm trying to program a player piano, but the problem is that it plays all of the same note before moving onto the next one.
private boolean isDown;
private String key;
private String sound;
private String upImage;
private String downImage;
private boolean explode;
private int playPosition;
private static final int NUM_FRAGMENTS = 100;
private int[] position =
{ 12, 12, 11, 10, 12, 10, 11, 12, 12, 11, 10, 12, 5 };
private boolean readyToPlay = true;
private int step = 0;
public Key(String keyName, String soundFile, String img1, String img2, boolean canExplode, int position)
{
sound = soundFile;
key = keyName;
upImage = img1;
downImage = img2;
setImage(upImage);
isDown = false;
explode = canExplode;
playPosition = position;
}
public void act()
{
player();
if (!isDown && Greenfoot.isKeyDown(key)) {
play();
setImage(downImage);
isDown = true;
//if (explode == true)
//{
// explode();
//}
}
if (isDown && !Greenfoot.isKeyDown(key)) {
setImage(upImage);
isDown = false;
}
}
public void player()
{
for (int i=0; i<13; i++)
{
if(playPosition == position[i] && readyToPlay == true)
{
readyToPlay=false;
setImage(downImage);
play();
Greenfoot.delay(8);
setImage(upImage);
Greenfoot.delay(8)
readyToPlay = true;
}
}
}
public void play()
{
Greenfoot.playSound(sound);
}
public void explode()
{
placeDebris (getX(), getY(), NUM_FRAGMENTS);
getWorld().removeObject(this);
}
private void placeDebris(int x, int y, int numFragments)
{
for (int i=0; i < NUM_FRAGMENTS; i++) {
getWorld().addObject ( new Debris(), x, y );
}
}
}
