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

2012/10/5

Attempting to have one key class.

kaciedy kaciedy

2012/10/5

#
Piano World: public class Key extends Actor { private String image1; private String image2; private boolean isDown;//true: key is down...false: key is up private String keyboard; private String soundFile; /** * , Computer Science Math 2012, java. * Relates the images and strings */ public Key(String k, String sound, String keyUp, String keyDown) { { keyboard = k; sound = soundFile; image1 = keyUp; image2 = keyDown; setImage(image1); } } /** * , Computer Science Math 2012, java. * Do the action for this key. */ public void act() { animate(); } /** * , Computer Science Math 2012, java. * animates the keys */ public void animate() { if ( !isDown && Greenfoot.isKeyDown(keyboard)) { play(); setImage (image2); isDown = true; } if ( isDown && !Greenfoot.isKeyDown(keyboard)) { setImage (image1); isDown = false; } } /** * , Computer Science Math 2012, java. * play the sound file when key is clicked or when a key and space is clicked. */ public void play() { if(!Greenfoot.isKeyDown("space") && Greenfoot.isKeyDown(keyboard)) { Greenfoot.playSound(soundFile); } } } KEY CLASS: public class Piano extends World { private String letters = {"a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'","enter"}; private String files = {"3c.wav", "3d.wav", "3e.wav", "3f.wav", "3g.wav", "3a.wav", "3b.wav", "4c.wav", "4d.wav", "4e.wav", "4f.wav", "4g.wav"}; private String lower = {"2c.wav", "2d.wav", "2e.wav", "2f.wav", "2g.wav", "2a.wav", "2b.wav", "3c.wav", "3d.wav", "3e.wav", "3f.wav", "3g.wav"}; private String bLetters = {"w", "e", "", "t", "y", "u", "", "o", "p"}; private String bFiles = {"3c#.wav", "3d#.wav", "", "3f#.wav", "3g#.wav", "3a#.wav", "", "4c#.wav", "4d#.wav"}; private String bLower = {"2c#.wav", "2d#.wav", "", "2f#.wav", "2g#.wav", "2a#.wav", "", "3c#.wav", "3d#.wav"}; /** * , Computer Science Math 2012, java. * Make the piano. */ public Piano() { super(800, 340, 1); makeKeys(); showMessage(); } /** * , Computer Science Math 2012, java. * Makes the white and black keys. */ public void makeKeys() { int i = 0; while(i < 12) { if(letters != "") { Key key = new Key(letters, files, "white-key.png", "white-key-down.png"); addObject(key,54 + (i*63), 140); i++; } } int a = 0; while(a < 9) { if(bLetters != "") { Key key = new Key(bLetters, bFiles, "black-key.png", "black-key-down.png"); addObject(key,150 + (a*63), 86); } a++; } } /** * , Computer Science Math 2012, java. * Shows a message in red at the bottom of the world. */ public void showMessage() { GreenfootImage bg = getBackground(); bg.setColor(Color.GREEN); bg.drawString("Click 'Run', then use a,s,d,f,g,h,j,k,l,;,',enter,q,w,e,t,y,u,… to make sounds.", 25, 320); } } The code compiles, however, when I go to click a key, an error pops up "java.lang.NullPointerExecption: Filename must not be null. at Key.play(Key,java:64) at Key.animate(Key.Java:43) at Key.act(Key.java:32)" Can anyone please explain what I am doing wrong? I have tried to find new outlets and nothing is working.
danpost danpost

2012/10/5

#
In your Key constructor, you have
sound = soundFile;
where you really meant
soundFile = sound;
You need to login to post a reply.