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

2015/3/24

Need help with chapter 5

kyles247 kyles247

2015/3/24

#
So i was making the piano from the book scenarios (class project) and when i got it just about done, it wont compile.. what am i doing wrong?
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
public class Key extends Actor
{
    private boolean isDown;
    private String key;
    private String sound;
    private String keyDown;
    private String keyUp;
    /**
     * Create a new key linked to a keyboard key
     */
    public Key(String keyName, String soundFile, String Img1, String Img2)
    {
        key = keyName;
        sound = soundFile;
        keyDown = Img1;
        keyUp = Img2;
        setImage(keyUp);
    }
 
    /**
     * Do the action for this key.
     */
    public void act()
    {
        if ( !isDown && Greenfoot.isKeyDown(key)) {
         setImage(keyDown);
         playSound();
         isDown = true;
        }
        if ( isDown && !Greenfoot.isKeyDown(key)) {
            setImage (keyUp);
            isDown = false;
        }
    }
    
    /**
     * play a key
     */
     public void playSound()
     {
         Greenfoot.playSound(sound);
        }
}
piano class
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A piano that can be played with the computer keyboard.
 * 
 * @author: Kyle Strauser
 * @version: 0.1
 */
import greenfoot.*;
import java.awt.Color;
public class Piano extends World
{
    private String[] whiteKeys = 
    {"a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "\\" };        
    private String[] whiteNotes = 
    {"3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c", "4d", "4e", "4f", "4g"};
    private String[] blackKeys =
    { "W", "E", "", "T", "Y", "U", "", "O", "P", "", "]" }; 
    private String[] blackNotes =
    { "3c#", "3d#", "", "3f#", "3g#", "3a#", "", "4c#", "4d#", "", "4f#" }; 
    /**
     * Make the piano.
     */
    public Piano() 
    {
        super(800, 340, 1);
        makeKeys();
        makeBlackKeys();
    }
    /**
     * make the piano keys
     */
    public void makeKeys()
    {
     //white keys
     int i = 0;
     while (i < whiteKeys.length) {
        Key key = new Key(whiteKeys[i], whiteNotes[i] + ".wav", "white-key-down.png", "white-key.png");
        addObject(key, i * 63 + 54, 140);
        i = i + 1;
     }
   }
   
    /**
     * make the black keys bro
     */
    public void makeBlackKeys()
    {
    int i = 0;
    while (i < blackKeys.length) {
     if (! "".equals(blackKeys[i])) {
        Key key = new Key(blackKeys[i], blackNotes[i] + ".wav", "black-key-down.png", "black-key.png");
        addObject(key, i * 63 + 86, 80);
     }
    }
    i = i + 1;
   }
}
kyles247 kyles247

2015/3/24

#
when i press compile it do but wont show piano or anything
danpost danpost

2015/3/24

#
First, remove line 1 in your Piano class (it is a duplicate import statement). Upon quick inspect, I did not anything else amiss with the code. If the problem is not resolved at this point, go to the menubar and select 'Controls>Show debugger' and click on the Terminate button. Hopefully it will resolve itself from that.
danpost danpost

2015/3/24

#
Wait, found it. your 'i' increment in your 'makeBlackKeys' method is not inside the 'while' block where it needs to be.
kyles247 kyles247

2015/3/24

#
int i = 0;
    while (i < blackKeys.length) {
     if (! "".equals(blackKeys[i])) {
        Key key = new Key(blackKeys[i], blackNotes[i] + ".wav", "black-key-down.png", "black-key.png");
        addObject(key, i * 63 + 86, 80);
     }
    }
    i = i + 1;
so in this co
while (i < blackKeys.length)i = i + 1; {
de i need to put the i=i +0 inside while block like this? just tried and still having problems
kyles247 kyles247

2015/3/24

#
danpost wrote...
Wait, found it. your 'i' increment in your 'makeBlackKeys' method is not inside the 'while' block where it needs to be.
danpost danpost

2015/3/24

#
You only needed to switch lines 55 and 56.
kyles247 kyles247

2015/3/25

#
That did it thank you so much!
You need to login to post a reply.