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

2014/8/2

Piano - almost there

akirashindo39 akirashindo39

2014/8/2

#
Hi! I hope you are all enjoying the summer so far. I am working on the piano scenario. I am having issues with the black keys. I went to the book scenarios and opened the folder "piano-complete". If you look at that scenario, you will see that there is no class called "black keys". Is this how I am supposed to do this? I was wondering if you need the "black keys" class in order to make black keys on the piano. Right now, I have the white keys in the right place. For the black keys, I have the keys in the right place, but each key appears a white key, not a black key. So all I need to do now is to fix the image of those keys as black keys, not white keys. But I don't know how to do that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
 
/**
 * 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 =
        { "2c", "2d", "2e", "2f", "2g", "2a", "2b", "3c", "3d", "3e", "3f", "3g" };
         
    private String[] blackKeys =
        { "w", "e", "", "t", "y", "u", "", "o", "p", "", "[" };
     
    private String[] blackNotes =
        { "2c#", "2d#", "", "2f#", "2g#", "2a#", "", "3c#", "3d#", "", "3f#" };
         
    /**
     * Make the piano.
     * This adds 12 white piano keys to the world.
     */
    public Piano()
    {
        super(800, 340, 1);
        makeKeys();
        showMessage();
    }
    /**
     * Create the piano keys and place them in the world.
     */
    private void makeKeys()
    {
        for(int i = 0; i < whiteKeys.length; i++) {
            Key key = new Key(whiteKeys[i], whiteNotes[i] +".wav");
            addObject (key, i*63 + 54, 140);
        }
         
        for(int i = 0; i < whiteKeys.length-1; i++) {
            if( ! blackKeys[i].equals("") ) {
                Key key = new Key(blackKeys[i], blackNotes[i] +".wav");
                addObject(key, 85 + (i*63), 86);
            }
        }
             
    }
     
    public void showMessage()
    {
        GreenfootImage bg = getBackground();
        bg.setColor(Color.WHITE);
        bg.drawString("Click 'Run', then use your keyboard to play", 25, 320);
    }
}
sengst sengst

2014/8/4

#
in the constructor for key, pass in a String that is either "black" or "white". have the keys set their images using this String. if you do this the rest should sort of fall in place a bit easier. here's something that you could use: public key( string label, (...anything else you/they have) ) { if ( label == "black" ) setImage( the black image); if ( label == "white" ) setImage( the white image ); // anything else. } if it is the key the key is linked to is pressed, it will use the same type of thing: if ( Greenfoot.isKeyDown( key ) { if ( label == "black" ) setImage( the black image down ); if ( label == "white" ) setImage( the white image down ); } Good luck! Once the piano works it really is fun!! Hope this helps!
You need to login to post a reply.