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

2011/9/13

Piano 1 book scenario

carterfootball carterfootball

2011/9/13

#
Hi im trying to create black keys in The piano 1 book scenario but im having some trouble. My teacher says that we have to create black keys without creating a new class and that have to use a while loop in the Piano class to do so. Can someone help me?
AwesomeNameGuy AwesomeNameGuy

2011/9/13

#
Let's see how would I do this. I assume you already have a key class. So within your key class I would have a constructor or something which determines whether it's a white or black key. Then the next step is to set the image depending on which. So have an image for both black and white keys in your class and set the approperate image. In your constructor you probabily want to specify what sound the key makes somhow too, based on what kind of key it is. Then there's several ways I can think of to use a while loop in the piano class to put the keys in the right place. The way I would probabily try first would be to have a couple methods setWhiteKey() and setBlackKey() in the piano class and use those in the loop. With variables to keep track of how many keys have been set, either in the piano class or a static variables in the key class. Something like this for example while (numofkeys < max) { if (key.isblack()) { setBlackKey(); } else { }
AwesomeNameGuy AwesomeNameGuy

2011/9/13

#
oops I think I accidently created the post before I was done writing. Anyway, let me continue where I left off. while (x< max) { if (key.isblack()) { setBlackKey(); // this deals with setting the location of the black keys } else { setWhiteKey(); } x++; // x is the number of keys } Something like that for example. If left to my own devices I would do it differently, but this should conform to your instructions. I hope this helped set you in the right direction.
carterfootball carterfootball

2011/9/14

#
i finally got he piano to host black keys. but i had to use nulls to space the black keys out. once i did this it created black keys that did not do anything at all. I had to create and if statement saying that if there is a null then dont create a key. But now the that i have it compliling right my world disappear when i try to complie it on the scenario page. Here is my code for the piano class. I appreciate the help public class Piano extends World { private String whitebuttons = {"a","s","d","f","g","h","j","k","l",";","'","enter"}; private String whitesoundfiles = {"3a","3b","3c","3d","3e","3f","3g","4c","4d","4e","4f","4g"}; private String blackbuttons = {"w","e","","t","y","u","","o","p"}; private String blacksoundfiles = {"3c#","3d#","","3f#","3g#","3a#","","4c#","4d#"}; private boolean nulldeleter = {false, false, true, false, false, false, true, false, false}; /** * Make the piano. */ public Piano() { super(800, 340, 1); makeKeys(); } public void makeKeys() { int i = 0; //counter for white keys while (i < 12) { addObject(new Key(whitebuttons, whitesoundfiles+".wav", "white-key.png", "white-key-down.png"), 63*i + 54, 100); i++; } int x = 0; // counter for black keys while (x < 9) { if(nulldeleter!=true) { addObject(new Key(blackbuttons, blacksoundfiles+".wav", "black-key.png", "black-key-down.png"), 64*x + 148, 80); x++; } } } }
AwesomeNameGuy AwesomeNameGuy

2011/9/14

#
It's because the x is only incrementing if nulldeleter is false. It never reaches 9, and the program gets stuck in the while loop. Move the x++; out of the if statement and it should work I think. If you are like me, then you will make lots and lots of little mistakes like this as you try to write programs. But you will make less of them as time goes on!
carterfootball carterfootball

2011/9/14

#
I moved the x++ out of the if statement and it still compiles correctly but it acts like it is still stuck in a continuous loop.
carterfootball carterfootball

2011/9/14

#
figured it out deleted tyhe nulldeleter variable and used blackbuttons instead Here is my new code I figured out that world not coming up is a coputer error. The computer was having a hard time with the programming. Dont know why but it was public class Piano extends World { private String whitebuttons = {"a","s","d","f","g","h","j","k","l",";","'","enter"}; private String whitesoundfiles = {"3a","3b","3c","3d","3e","3f","3g","4c","4d","4e","4f","4g"}; private String blackbuttons = {"w","e","","t","y","u","","o","p"}; private String blacksoundfiles = {"3c#","3d#","","3f#","3g#","3a#","","4c#","4d#"}; /** * Make the piano. */ public Piano() { super(800, 340, 1); makeKeys(); } public void makeKeys() { int i = 0; //counter for white keys while (i < 12) { addObject(new Key(whitebuttons, whitesoundfiles+".wav", "white-key.png", "white-key-down.png"), 63*i + 54, 100); i++; } int x = 0; // counter for black keys while (x < 9) { if(blackbuttons != "") { addObject(new Key(blackbuttons, blacksoundfiles+".wav", "black-key.png", "black-key-down.png"), 64*x + 148, 80); } x++; } } }
carterfootball carterfootball

2011/9/14

#
The x++ did help though I appreciate the help
You need to login to post a reply.