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

2015/9/2

Making An On Screen Piano - Chapter 5; Part 4

ConG36C ConG36C

2015/9/2

#
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A piano that can be played with the computer keyboard.
 * 
 * @author: M. Kolling
 * @version: 0.1
 */
public class Piano extends World
{
    /**
     * Make the piano.
     */
    private String[] whitekeys = 
        {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]"};
    private String[] whiteNotes = 
        {"2c", "2d", "2e", "2f", "2g", "2a", "2b", "3c", "3d", "3e", "3f", "3g"};
    
    public Piano() 
    {
        super(800, 340, 1);
        makekeys();
    }

    /**
     * This puts all of the keys where they should go.
     */
    public void makekeys()
    {
        int i = 0;
        while (i < 12)
        {
            Key whiteKeys = new Key(whiteKeys[i], whiteNotes[i] + ".wav");
            int keyWidth = myKey.getImage().getWidth();
            int keyHeight = myKey.getImage().getHeight();
            int worldWidth = getWidth();
            addObject(myKey, i*keyWidth + (((worldWidth - (keyWidth * 12))/2) + (keyWidth / 2)), keyHeight / 2);
            i = i + 1;
        }    
    }
}
Ok when I compile this, the 'i' after '(whiteKeys import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class Key extends Actor { private String key; private String soundfile; private boolean isDown; /** * Create a new key. */ public Key(String newkey, String newsoundfile) { key = newkey; soundfile = newsoundfile; setImage("white-key.png"); } /** * Do the action for this key. */ public void act() { if ( !isDown && Greenfoot.isKeyDown(key) ) { setImage ("white-key-down.png"); Greenfoot.playSound(soundfile); isDown = true; } if ( isDown && !Greenfoot.isKeyDown(key) ) { setImage ("white-key.png"); isDown = false; } } } Thank you all very much!
Super_Hippo Super_Hippo

2015/9/2

#
In line 33, you start with 'Key whiteKeys'. Shouldn't it be 'Key myKey'? You use 'myKey' in the next lines and 'whiteKeys' is the name of your array.
You need to login to post a reply.