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

2012/2/19

Chapter 5 abstraction Building the piano

sophiasturges sophiasturges

2012/2/19

#
Hello--I'm totally lost! I am trying to add fields for the key and sound file, as well as a constructor with two parameters that initializes those fields. When I try to add a new key or add a new object, the program asks me for a ";" sign, which is already there, or doesn't allow me to shift click the object into the world. I don't know where to place my add object or new key to my code to make this work. This is what I have so far: public class Key extends Actor { private boolean isDown; private String key; private String sound; /** * Create a new key linked to a given keyboard key, and * with a given sound. */ public Key(String keyName, String soundFile) { key = keyName; sound = soundFile; new Key ("a", "2a.wav"); } /** * Do the action for this key. */ public void act() { if ( !isDown && Greenfoot.isKeyDown("a") ) { play(); setImage ("white-key-down.png"); isDown=true; } if ( isDown && !Greenfoot.isKeyDown("a") ) { setImage ("white-key.png"); isDown=false; } } /** * Play the note of this key. */ public void play () { Greenfoot.playSound("2a.wav"); } }
Game/maniac Game/maniac

2012/2/19

#
Try this public void act() { if ( !isDown && Greenfoot.isKeyDown("a") ) { play(); setImage ("white-key-down.png"); isDown=true; }else{ setImage ("white-key.png"); isDown=false; } }
davmac davmac

2012/2/19

#
Can you post the code that gives you the error message? (or, if it's the code above, which line does the error occur on?)
sophiasturges sophiasturges

2012/2/19

#
Thanks Game/maniac. That didn't work though. I appreciate you trying to help!
sophiasturges sophiasturges

2012/2/19

#
Hi davmac, This was my code prior... I was able to create keys by shift clicking into the world and they played the 3a.wav sound when I pressed the keyboard letter "g." import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class Key extends Actor { private boolean isDown; /** * Create a new key linked to a given keyboard key, and * with a given sound. */ public Key() { } /** * Do the action for this key. */ public void act() { if ( !isDown && Greenfoot.isKeyDown("g") ) { play(); setImage ("white-key-down.png"); isDown=true; } if ( isDown && !Greenfoot.isKeyDown("g")){ setImage ("white-key.png"); isDown=false; } } /** * Play the note of this key. */ public void play () { Greenfoot.playSound("3a.wav"); } }
sophiasturges sophiasturges

2012/2/19

#
My next instructions are where I get stuck. I'm supposed to add fields for the key and sound file and add a constructor with two parameters that initialize those fields. This is the start: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class Key extends Actor { private boolean isDown; private String key; private String sound; /** * Create a new key linked to a given keyboard key, and * with a given sound. */ public Key() { key = keyName; sound = soundFile; } /** * Do the action for this key. */ public void act() { if ( !isDown && Greenfoot.isKeyDown("g") ) { play(); setImage ("white-key-down.png"); isDown=true; } if ( isDown && !Greenfoot.isKeyDown("g")){ setImage ("white-key.png"); isDown=false; } } /** * Play the note of this key. */ public void play () { Greenfoot.playSound("3a.wav"); } }
sophiasturges sophiasturges

2012/2/19

#
I tried placing this code: addObject (new Key ("g", "3a.wav"), 300, 180); to the constructor under key = keyName; sound = soundFile; It will only compile if I add the code: new Key ("g","3a.wav") but then it won't create any keys. I'm not sure where to place this creating object part of my code. I'm not sure if this makes any sense. I appreciate any advice as I have asked everyone I know and no one seems to know how to help! Thanks, Sophie
danpost danpost

2012/2/19

#
You need to create the key objects and add them to the world from the World class.
// (in world class constructor or a prepare() method called from the constructor)
addObject(new Key("a", "2a.wav"), x1, y);
addObject(new Key("s", "2b.wav"), x2, y);
// etc.
where x1 and x2 have values for the x-location of the keys and y has a value for the y-location of the white keys (you could use the actual values or expressions). @Game/maniac, with the code you provided, while the key is held down, the value of isDown will continuously alternate true to false and back. Follow the code and work it out. Actually, what sophiasturges posted looked good, except for the one line 'new Key("a", "2a.wav");' which should be removed from said code.
davmac davmac

2012/2/19

#
Well, you shouldn't create a new Key from the Key constructor anyway - probably, you meant to do that from the world class constructor (PianoWorld or whatever it is called). You can also call addObject() from within the world constructor and it won't give an error. Explanation: The Key constructor is called whenever you create a "new Key" - so if you create a new Key from inside the Key constructor, it will then call the Key constructor again, infinitely...! (well, you will eventually get an exception). Also, the "addWorld" method is a world method so you can't call it from Key (which is an actor) without qualifying it with a world reference (which isn't what you want to do here anyway).
sophiasturges sophiasturges

2012/2/19

#
Thank you danpost and davmac. I will try these suggestions out. :)
danpost danpost

2012/2/19

#
@sohpiasturges, the constructor declaration with the two parameters in your first post was good. I noticed you removed the parameters in your last post.
sophiasturges sophiasturges

2012/2/20

#
@danpost, When I add: public Key (String keyName, String soundFile) { key = keyName; sound = soundFile; } I am no longer able to create keys with the keyboard world. I'm not sure why. This is where I was stuck. I don't know if I need to add a method now? This is why I was trying to addObject. I understand that is supposed to be placed in the Piano constructor now, rather than the Key actor constructor. I don't know what I've done that has caused my code to stop making keys. Argh.
sophiasturges sophiasturges

2012/2/20

#
@danpost, By the way, it does compile when I add this to my code.
sophiasturges sophiasturges

2012/2/20

#
@danpost & davmac: Your suggestion to addObject from the Piano world did work. :) Thanks!
You need to login to post a reply.