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

2015/12/2

Greenfoot Piano Part 5

andrewstouse andrewstouse

2015/12/2

#
Hello Dan post, I am currently on part 5 of greenfoot chapter 5 piano part 5. I have run into a problem and i cannot seem to figure out why it is wrong. Below is my code for the piano class, when i compile it highlights "whiteKeyWidth" 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 = { "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "" }; private String whiteNotes = { "3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c", "4d", "4e", "4f", "4g" }; private String blackKeys = { "2", "3", "", "5", "6", "7", "", "9", "0", "", "=", "c"}; private String blackNotes = { "3c#", "3d#", "3e#", "3f#", "3g#", "3a#", "3a#", "4c#", "4d#", "4d#", "4f#", "4f#" }; /** * Piano's Constructor */ public Piano() { super(800, 340, 1); addKeys(); addblackKeys(); makeKeys(); } /** * Create the piano keys and place them in the world. */ private void addKeys() { int i = 0; while (i < whiteKeys.length) { Key mykey = new Key(whiteKeys, whiteNotes+".wav", "white-key.png", "white-key-down.png"); // get the image from the key and ask it what its width is int keyWidth = mykey.getImage().getWidth(); whitekeyWidth = keyWidth; // get the image from the key and ask it what its height is int keyHeight = myKey.getImage().getHeight(); //we're in Piano, which is a World, so ask it what its width is int worldWidth = getWidth(); int offset = ((worldWidth - (12* keyWidth))/2)+(keyWidth/2); addObject (myKey, i*keyWidth + offset, keyHeight/2); i = i = 1; } } private void addblackKeys() { int i = 0; while (i < blackKeys.length) { if ( ! blackKeys.equals("") ) { Key mykey = new Key(blackKeys, blackNotes+".wav", "black-key.png", "black-key-down.png"); int keyWidth = myKey.getImage().getWidth(); blackKeyWidth = keyWidth; int keyHeight = myKey.getImage().getHeight(); int worldWidth = getWidth(); int offset = ((worldWidth - (12* keyWidth))/2)+(keyWidth/2); addObject (myKey, i*keyWidth + offset, keyHeight/2); i = i = 1; } } } public void makeKeys() { GreenfootImage bg = getBackground(); bg.setColor(Color.PINK); bg.drawString("Click Run, then use your keyboard to play"); } }
Super_Hippo Super_Hippo

2015/12/2

#
1. Use code tags when posting code! 2. whitekeyWidth is in the code only once. (Same with blackkeyWidth.) The questions are: Do you need this variable? If yes, for what? You could write 'private int whitekeyWidth, blackkeyWidth' outside methods if you want to save the values to these fields.
You need to login to post a reply.