-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFadingText.java
More file actions
78 lines (69 loc) · 2.31 KB
/
Copy pathFadingText.java
File metadata and controls
78 lines (69 loc) · 2.31 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Text that can either fade in and out or stay on screen.
*
*/
public class FadingText extends Actor
{
private GreenfootImage image;
private static final Color transparent = new Color(0, 0, 0, 0);
private static final Color textColor = Color.WHITE;
private int transparency, maxTransparency, actsLeft, diffPerAct, actsDisplayed, actsToDisplay;
private static final int ACTS_TO_FADE = 50;
private boolean fade, isFadingIn, isFadingOut;
public FadingText(String text, boolean fade, int actsToDisplay){
image = new GreenfootImage(text, 20, textColor, transparent);
setImage(image);
actsLeft = ACTS_TO_FADE;
actsDisplayed = 0;
diffPerAct = (int) 255 / ACTS_TO_FADE;
maxTransparency = diffPerAct * ACTS_TO_FADE;
this.fade = fade;
this.actsToDisplay = actsToDisplay;
//if fading in and out, start from transparent image
if (fade == true){
transparency = 0;
image.setTransparency(transparency);
isFadingIn = true;
isFadingOut = false;
}
}
public void act(){
if (fade == true){
if (isFadingIn){
fadeIn();
} else {
//after faded in: increase acts displayed until text has been displayed for actsToDisplay, then fade out
actsDisplayed++;
if (actsDisplayed >= actsToDisplay){
fadeOut();
}
}
}
//else do nothing
}
private void fadeIn(){
//increase transparency by diffPerAct
actsLeft--;
if (actsLeft > 0){
transparency += diffPerAct;
image.setTransparency(transparency);
setImage(image);
} else {
//reset actsLeft to fade out
isFadingIn = false;
actsLeft = ACTS_TO_FADE;
}
}
private void fadeOut(){
//decrease transparency by diffPerAct
actsLeft--;
if (actsLeft > 0){
transparency -= diffPerAct;
image.setTransparency(transparency);
setImage(image);
} else {
getWorld().removeObject(this);
}
}
}