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

2018/1/28

HELP

Indy5Drew Indy5Drew

2018/1/28

#
I am trying to make a piano that will play a song by itself, and the keys will go down to correspond with the note being played. I am trying to name the key so that I can call on it when the song is being played, so that it will go down when needed. Does anyone know how? the segment of code I am trying to name is: addObject (new Key("a", "3c.wav"), 53, 108);
danpost danpost

2018/1/28

#
You can break the line down as such:
Key key = new Key("a", "3c.wav");
addObject(key, 53, 108);
which now gives you a reference to the key (in 'key'). Then, you can move the first line outside the constructor (or method) so that the reference exists beyond the execution of the block of code and now exists for as long as the World object exists. As such, it is an 'instance field' as opposed to a 'local variable'. The field can be expanded into an array to hold all your keys, which you will load as you create the keys during construction of the world. From there, the placement of the keys in the array give an index value to each key. A string like the following "abcdefgABC" can be used to show the order of keys in the array. The 'indexOf(String)' method (where the String parameter given is a single key -- one of the characters in the string of key names) used on this string will return the index value needed to get the proper key in the array.
Indy5Drew Indy5Drew

2018/1/29

#
Thanks! I have another question: So I got the keys to work by creating a bunch of classes, and I am able to press "z" and it plays low C. It is supposed to then play the rest of the notes to twinkle twinkle little star, at least the is my plan. My problem is the delay between the keys. Any ideas on how to put like one second between each note, and how to make the key change to "white-key.png" after the note is played? here is what i have so far... if ( Greenfoot.isKeyDown("z") ) { setImage ("white-key-down.png"); play(); isDown = true; } if (isDown = true) { setImage ("white-key.png"); isDown = false; } I need to add a delay in between the key going down and playing the note, the key going up, and then the next note being played. My only problem is that when the key played, the key doesn't go down and the notes play at the same time after a delay, which i do not want.
Indy5Drew Indy5Drew

2018/1/29

#
also, I was wondering how I would make it where the other keys realize that the first key was pressed, an then the next key plays. Any Ideas?
Vercility Vercility

2018/1/30

#
Use a counter for delaying (I read somewhere that 130 act cycles are about 1sec but you'd have to try yourself) And one for the amount of keys played yet When the Counter is > whatever delay u want, call a method that plays a key depending on the key counter
You need to login to post a reply.