Below is all of the Actors / code that is in my Greenfoot (Java) Scenario
First is the code for the black keys of my piano:
Next is the code for the piano world:
Then I have the code for my white keys:
And lastly, a direct copy of the output in the Greenfoot Terminal:
I have yet to figure the problem out. There are no given errors in the Greenfoot code editor so I feel like it should work...
And I should mention that the errors are given as soon as I click the "run" button to start the code
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Black_Key here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Black_Key extends Actor
{
/**
* Create a new key.
*/
public String black_key;
private String black_sound;
public Black_Key(String keyName, String soundFile) { //won't work without "void" return type declaration
black_key = keyName;
black_sound = soundFile;
}
String[] blackKeys = {"W", "E", "T", "Y", "U", "O", "P", "]"};
String[] blackNotes = {"3c#", "3d#", "3f#", "3g#", "3a#", "4c#", "4d#", "4f#"};
/**
* Do the action for this key.
*/
public void act() {
animateBlackKey(); // throws errror because of line in method
}
public boolean isDown2;
private void playSound() {
Greenfoot.playSound(black_sound);
}
public void animateBlackKey() {
if(!isDown2 && Greenfoot.isKeyDown(black_key)){ //throws unknown error that key is blank
setImage("black-key-down.png");
isDown2 = true;
playSound();
}
if(isDown2 && !Greenfoot.isKeyDown(black_key)){
setImage("black-key.png");
isDown2 = false;
}
}
}
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
{
/**
* Make the piano.
*/
public Piano()
{
super(800, 340, 1);
displayWhiteKeys();
displayBlackKeys();
}
//White keys
public String[] whiteKeys = {"a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "/"};
public String[] whiteNotes = {"3a", "3b", "3c", "3d", "3e", "3f", "3g", "4c", "4d", "4e", "4f", "4g"};
//Black keys
String[] blackKeys = {"W", "E", "", "T", "Y", "U", "", "O", "P", "", "]"};
String[] blackNotes = {"3c#", "3d#", "", "3f#", "3g#", "3a#", "", "4c#", "4d#", "", "4f#"};
public void displayWhiteKeys() {
int i = 0;
while (i < 12) {
Key key = new Key(whiteKeys[i], whiteNotes[i] + ".wav");
addObject (key, 54 + (i*key.getImage().getWidth()), 140);
i++;
}
}
public void displayBlackKeys() {
int x = 0;
int l;
while(x < 8) {
Black_Key bkey = new Black_Key(blackKeys[x], blackNotes[x] + ".wav");
addObject (bkey, 150 + (x*63), 85); // replace l with number for spacing
x++;
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
public class Key extends Actor
{
/**
* Create a new key.
*/
private String key;
private String sound;
public Key(String keyName, String soundFile) {
key = keyName;
sound = soundFile;
}
String[] whiteKeys = {"A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "/"};
String[] whiteNotes = {"3a", "3b", "3c", "3d", "3e", "3f", "3g", "4c", "4d", "4e", "4f", "4g"};
/**
* Do the action for this key.
*/
public void act() {
animateKey();
}
public boolean isDown;
public void playSound() {
Greenfoot.playSound(sound);
}
public void animateKey() {
if(!isDown && Greenfoot.isKeyDown(key)){
setImage("white-key-down.png");
isDown = true;
playSound();
}
if(isDown && !Greenfoot.isKeyDown(key)){
setImage("white-key.png");
isDown = false;
}
}
}Greenfoot Terminal wrote...
java.lang.IllegalArgumentException: "" key doesn't exist. Please change the key name while invoking Greenfoot.isKeyDown() method
at greenfoot.gui.input.KeyboardManager.isKeyDown(KeyboardManager.java:237)
at greenfoot.Greenfoot.isKeyDown(Greenfoot.java:97)
at Black_Key.animateBlackKey(Black_Key.java:36)
at Black_Key.act(Black_Key.java:28)
at greenfoot.core.Simulation.actActor(Simulation.java:604)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
at greenfoot.core.Simulation.runContent(Simulation.java:221)
at greenfoot.core.Simulation.run(Simulation.java:211)
