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

2014/4/4

Help with Programming a Player Piano?

Sneaky4296 Sneaky4296

2014/4/4

#
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 );
       }
    } 
}
Sneaky4296 Sneaky4296

2014/4/4

#
Also, ignore the code for exploding. It was for a previous assignment in this class and I haven't cleaned up the code yet.
danpost danpost

2014/4/6

#
The problem is that you are trying to use the Key class to control all the keys. A class is a blueprint of an object and the methods within it should be each geared for a single key. If you want to control how all the keys are to behave, the world class is where that code should probably go. It may require a dual set of conditions in your act method of the Key class for having a Key object play its note (the set you already have, plus one more set for the player). Maybe add an 'int' field to the class for 'autoPlay' and add a method for the world to call to set it to some nominal value (maybe 20). Then your condition in the act could be:
if (!isDown && (Greenfoot.isKeyDown(key) || autoPlay > 0)
For the second 'if' block, use these conditions:
if (isdown && !Greenfoot.isKeyDown(key) && autoPlay == 0)
{
    // code you already have
}
// add this third 'if' block
if (autoPlay > 0)
{
    autoPlay--;
}
You need to login to post a reply.