-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDelayAudio.java
53 lines (48 loc) · 1.38 KB
/
DelayAudio.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
43
44
45
46
47
48
49
50
51
52
53
import greenfoot.*;
/**
* Write a description of class Timer2 here.
*
* @author (your name)
* @version (a version number or a date)
*/
class DelayAudio extends Actor
{
public GreenfootSound music;
public GreenfootSound stop;
public int volume;
public boolean loop;
public long deltaTime;
public long lastFrame = System.nanoTime();
public long currentFrame = System.nanoTime();
public long delayTime;
DelayAudio(GreenfootSound music, int volume, boolean loop, long delayTime) {
this.delayTime = delayTime;
this.music = music;
this.volume = volume;
this.loop = loop;
}
DelayAudio(GreenfootSound music, GreenfootSound stop, int volume, boolean loop, long delayTime) {
this.stop = stop;
this.delayTime = delayTime;
this.music = music;
this.volume = volume;
this.loop = loop;
}
public void act() {
currentFrame = System.nanoTime();
deltaTime = (currentFrame - lastFrame) / 1000000;
if (deltaTime > delayTime) {
if (stop != null) {
stop.stop();
}
music.setVolume(volume);
if (loop) {
music.playLoop();
} else {
music.play();
}
getWorld().removeObject(this);
return;
}
}
}