P.43 (Can Include Black Keys - Different Key Images)


// Add two more variables below
private String upImage;
private String downImage;
  
public Key(String keyName, String soundFile, String img1, String img2) {
  key = keyName;    
  sound = soundFile;
  upImage = img1;
  downImage = img2;
  setImage(upImage);
}

public void act() {
  if(!isDown && Greenfoot.isKeyDown(key)) {
    setImage(downImage);
    play();
    isDown = true;
  }
  if(isDown && !Greenfoot.isKeyDown(key)) {
    setImage(upImage);
    isDown = false;
  }
}
Last modified on
Engineering DIY Workshop 2018