New to Greenfoot. How do I modify the code of the Key class so that the keys respond separately. The code should use the new variables keyName and soundFile in its Act and Play classes, rather than hard-coded values like “3a.wav” and “g”.
Thanks
public Key extends Actor { private String keyName = ""; private String soundFile = ""; // This constructor will recieve the information for the two variables and set those variables accordingly public Key(String name, String file) { keyName = name; soundFile = file; // any other code for constructor here } public void act() { if (Greenfoot.isKeyDown(keyName)) { // code to play soundFile here } else { // code to ensure soundFile is not playing (if it is, stop it here) } // any other act code you may have here (I do not think there would be any) } // other methods you may have here }
addObject(new Key("a","LowC.wav"), xLoc, yLoc);