/
Key.java
42 lines (37 loc) · 852 Bytes
/
Key.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 keyboard key with a given sound
*/
public Key(String keyName, String soundFile)
{
key = keyName;
sound = soundFile;
}
/**
* Do the action for this key.
*/
public void act()
{
if (!isDown && Greenfoot.isKeyDown(key)){
setImage("white-key-down.png");
isDown=true;
play();
}
if(isDown && !Greenfoot.isKeyDown(key)) {
setImage("white-key.png");
isDown=false;
}
}
/**
* Play the note of the key.
*/
public void play()
{
Greenfoot.playSound(sound);
}
}