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

2012/4/2

greenfoot piano (from the book)

wahaj wahaj

2012/4/2

#
im at the end of chapter 5 in the book and im stuck at creating the black piano keys. the given scenario uses for loops which arent introduced untill chapter 6. here is the code for the world class Piano. in the code Key is the actor class and key is a variable in the class Key. im supposed make black keys but they arent supposed to be next to each other like in a normal piano. this codes puts them next to each other like the white keys. i tried putting in an if statement. the class compiles but the greenfoot window just goes white. the if statement i used is if (! blackKeys.equals("") ). this statement is put in the while loop for the black keys. can you tell me whats going wrong?
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A piano that can be played with the computer keyboard.
 * 
 * @author: M. Kolling
 * @version: 0.4
 */
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  = { "q", "w", "", "t", "u", "", "o", "p", "", "]"};
    private String[] blackNotes = { "3c#", "3d#", "", "3f#", "3g#", "3a#", "", "4c#", "4d#", "", "4f#"};
        
    /**
     * Make the piano. This means mostly, apart from defining the size,
     * making the keys and placing them into the world.
     */
    public Piano() 
    {
        super(800, 340, 1);
        makeKeys();
    }
    
    /**
     * Create the piano keys and place them in the world.
     */
    private void makeKeys() 
    {
        int i = 0;
        while (i < whiteKeys.length) 
        {
            Key key = new Key(whiteKeys[i], whiteNotes[i] + ".wav" , "white-key.png", "white-key-down.png");
            addObject(key, 54 + (i*63), 140);
            i = i + 1;
        }     
        int a = 0;
        while (a< blackKeys.length )
        {                     
                
            Key key = new Key(blackKeys[a], blackNotes[a] + ".wav", "black-key.png", "black-key-down.png");
            addObject (key, 85 + (a*63), 86);
            a = a +1;       
        
        }
    }
}
mik mik

2012/4/2

#
Your idea with the empty string and the if-statement was very good! The only detail is that you should not check whether blackKeys is "", but whether the specific element in blackKeys is "". Like this if (! blackKeys.equals("") ) { ... place key here ... } a = a + 1; It's important that the increment of a is outside the if-statement. Michael
wahaj wahaj

2012/4/3

#
well i cant take credit for this. i did get these ideas from the book. but i understand now what to do. i think the major mistake that i was that i put the increment of a inside the if statement. my mind kind of went towards that but for some reason i didnt try it. thanks for the reply.
You need to login to post a reply.