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

2014/12/8

Help me please!

Epickiller10 Epickiller10

2014/12/8

#
So i was making the piano from the book scenarios (class project) and when i got it just about done, it wont compile.. it says no syntax errors but the world and keys simply will not generate, if someone could give me a bit of insite that would be much appreciated! code for the piano
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
{
    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 (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;
    }
}

}
}
code for the key
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;
    }

    /**
     * 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);
        }
}
danpost danpost

2014/12/8

#
If the 'Compile' button becomes clickable again, then the project compiled. Can you click on 'Run' at all?? if so, while running press a few keys that represent some of the notes and observe the behavior. Report back with any added information.
Epickiller10 Epickiller10

2014/12/8

#
The compile button is clickable, i must have forgot to include that. the project seems to compile alright now (restarting greenfoot fixed that) but run act and pause are greyed out, so i can not click any of them.. like i said the program was working nearly fine i was just tweaking the black key generating script when the class ended, i tested to be sure it worked which it did, saved it and exited when i got my computer home for the weekend and tried to do some more on it, the world just will not load. that is what confuses me because as i said it was working fine with the exact same code previously unless i accidentally added/removed something that i did not notice.
danpost danpost

2014/12/8

#
You could try commenting the entire world class and start with the following:
import greenfoot.*;

public class Piano extends World
{
    public Piano()
    {
        super(800, 340, 1);
    }
}
With this, compile the project. Then uncomment a few lines at a time, re-compiling along the way (first the fields -- lines 11 though 18; then the methods, one at a time). If there is a problem somewhere, you should be able to narrow down where it is located. It may not necessarily be in this class if the line that appears to cause the problem has a 'new' instance of a class created in it.
danpost danpost

2014/12/8

#
Line 48 does appear to be a possible problem. Try changing it to this:
if (! "".equals(blackKeys[i]))
I do not think that this is the cause of your incomplete compiling, however.
Epickiller10 Epickiller10

2014/12/9

#
ill give those a try! thanks for the help ill get back to this if it works/doesnt work :D
Epickiller10 Epickiller10

2014/12/9

#
So after doing what you suggested, i couldent really narrow it down because it is just simply not working. all that happens when i click compile is nothing and the code was unchanged from when it was working (besides line 48 now) but thanks for helping with line 48.
danpost danpost

2014/12/9

#
The problem is that line 51 in your Piano class is inside the if block of the while block; so 'i' does not get incremented when the key name is an empty string (therefore, an infinite loop is created). Move it down outside of the block it is currently in.
Epickiller10 Epickiller10

2014/12/9

#
ok thank you so much that fixed it! and another issue that has been happening is the black keys start out as white keys until played then they turn into black keys.. they all have their proper notes and such its just they appear as white keys untill they are executed here is an example the keys that are black in the picture are the ones i have played, and as you can see the rest along the right hand side are white. Thank you for your help so far i really appreciate it!
danpost danpost

2014/12/9

#
You just need to set its image in the Key class constructor. As a last line, put this one (insert at line 19):
setImage(keyUp);
Epickiller10 Epickiller10

2014/12/10

#
Thank you for the help! i appreciate it!!
You need to login to post a reply.