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

2014/8/27

Trying to make a Foot pedal on the piano project from the book.

GhostTheToast GhostTheToast

2014/8/27

#
Hey guys, How do you play Arrays? Here is the code:
public class Key extends Actor
{
    private boolean isDown;
    private String key;
    private String sound;
    private String upImage;
    private String downImage;
    private String [] lower =
         {"2c", "2d", "2e", "2f", "2g", "2a", "2b", "3c", "3d", "3e", "3g"};    
    /**
     * Create a new key linked to a given keyboard key, and
     * with a given sound.
     */
    public Key(String keyName, String soundFile, String img1, String img2)
    {
        sound = soundFile;
        key = keyName;
        upImage = img1;
        downImage = img2;
        setImage(upImage);
        isDown = false;  
    }
    /**
     * Do the action for this key.
     */
    public void act()
    {
        if (!isDown && Greenfoot.isKeyDown(key)) {
            play();
            setImage(downImage);
            isDown = true;
        }
        if (isDown && !Greenfoot.isKeyDown(key)) {
            setImage(upImage);
            isDown = false;
        }
        if (Greenfoot.isKeyDown("SPACE") && Greenfoot.isKeyDown(key)) {
            Greenfoot.playSound (lower+".wav");
        }
    }    
    /**
     * Play the note of this key.
     */
    public void play()
    {
        if (Greenfoot.isKeyDown("SPACE") && Greenfoot.isKeyDown(key)) {
            Greenfoot.playSound (lower+".wav");
        }
        Greenfoot.playSound(sound);         
    }
}
I'm probably missing something that will make me feel stupid. -GhostTheToast
danpost danpost

2014/8/28

#
Maybe the array should be in your world class and another argument added to the Key constructor for the 'lower' key String (like the 'key' field is set up).
GhostTheToast GhostTheToast

2014/8/28

#
@danpost ok, this is what I have
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
/**
 * A piano that can be played with the computer keyboard.
 * 
 * @author Michael Kolling
 * @version 1.0
 */
public class Piano extends World
{
    private String[] whiteKeys =
        { "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "ENTER" };
    private String[] whiteNotes =
        { "3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c", "4d", "4e", "4f", "4g" };
    private String [] whiteLow =
         {"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 =
        { "3c#", "3d#", "", "3f#", "3g#", "3a#", "", "4c#", "4d#", "", "4f#" };
    private String [] blackLow =
         {"2c#", "2d#", "", "2f#", "2g#", "2a#", "", "3c#", "3d#", "", "3f#"};
    /**
     * 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();
        showMessage();
    }
    /**if 
     * Create the piano keys (white and black) and place them in the world.
     */
    private void makeKeys() 
    {
        // make the white keys
        for(int i = 0; i < whiteKeys.length; i++) {
            Key key = new Key(whiteKeys[i], whiteNotes[i]+".wav", whiteLow[i] +".wav", "white-key.png", "white-key-down.png");
            addObject(key, 54 + (i*63), 140);
        }
        // make the black keys
        for(int i = 0; i < whiteKeys.length-1; i++) {
            if( ! blackKeys[i].equals("") ) {
                Key key = new Key(blackKeys[i], blackNotes[i]+".wav", blackLow[i] +".wav", "black-key.png", "black-key-down.png");
                addObject(key, 85 + (i*63), 86);
            }
        }            
    }
    public void showMessage()
    {
        GreenfootImage bg = getBackground();
        bg.setColor(Color.GREEN);
        bg.drawString("Can you handle the Piano?", 250, 320);
    }
}
and
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A key on a piano keyboard. This key has a pair of images to show a normal
 * and depressed state, and it is associated with a keyboard key and a sound
 * file, which is played when the key is pressed.
 * 
 * @author: M. Kolling
 * @version: 1.0
 */
public class Key extends Actor
{
    private boolean isDown;
    private String key;
    private String sound;
    private String upImage;
    private String downImage;
    private String Low;
            
    /**
     * Create a new key linked to a given keyboard key, and
     * with a given sound.
     */
    public Key(String keyName, String soundFile, String img1, String img2, String keyLow)
    {
        sound = soundFile;
        key = keyName;
        upImage = img1;
        downImage = img2;
        setImage (upImage);    
        isDown = false;
        Low = keyLow;
    }
    /**
     * Do the action for this key.
     */
    public void act()
    {
        if (!isDown && Greenfoot.isKeyDown(key)) {
            play();
            setImage(downImage);
            isDown = true;
        }
        if (isDown && !Greenfoot.isKeyDown(key)) {
            setImage(upImage);
            isDown = false;
        }        
    }    
    /**
     * Play the note of this key.
     */
    public void play()
    {
        if (Greenfoot.isKeyDown("space") && Greenfoot.isKeyDown(key)) {
            Greenfoot.playSound (Low);
        }
        Greenfoot.playSound(sound);         
    }
} 
This would work, but it keep saying cant find "2c.wav" and i've checked my folders. 2c.wav is in there -GhostTheToast
Super_Hippo Super_Hippo

2014/8/28

#
Could it be that the order of the parameters for the constructor is incorrect? You have
public Key(String keyName, String soundFile, String img1, String img2, String keyLow) 
but you create your Key with
new Key(whiteKeys[i], whiteNotes[i]+".wav", whiteLow[i] +".wav", "white-key.png", "white-key-down.png");
So in the constructor you have the key, a sound file, two images and another sound file, but you want to create a key with the key, two sound files and two images. Change the constructor to
public Key(String keyName, String soundFile, String keyLow, String img1, String img2) 
and see if it works then.
danpost danpost

2014/8/28

#
The order of the arguments is not going to make any difference provided you are passing the right ones in the right places and treating them as such. What might make a difference is the name. That is, it might be "2C.wav", "2c.WAV", "2C.WAV" or whatever. Make sure the names are case-wise exactly what they need to be.
GhostTheToast GhostTheToast

2014/8/29

#
@Super_Hippo you were right man, Nice and thank you so much! @danpost Na, I had it just as i typed, but i see where your coming from and thank you for your assitance. :) Have a great day guys and you'll probably see more of me. I just getting into greenfoot and so far I love it, but since I'm just getting use to it. I'll still have tons of questions. Anyways have a great day and thanks guys -GhostTheToast
danpost danpost

2014/8/29

#
@Super_Hippo, sorry if I made it seem your response was incorrect. If I had looked a little closer as to what you were pointing out, I would not have responded at all. In fact, if you notice, I qualified the apparent discounting with exactly what the problem was:
danpost wrote...
provided you are passing the right ones in the right places
Again, I apologize.
You need to login to post a reply.