-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHorizontalScrolling.java
76 lines (61 loc) · 2.49 KB
/
HorizontalScrolling.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import javafx.animation.AnimationTimer;
import javafx.scene.image.ImageView;
public class HorizontalScrolling {
private MainGame game;
private double screen = 0.0;
private double crossPos = 0.0;
private ImageView background;
private ImageView foreground;
private ImageView background1;
private ImageView foreground1;
private ImageView background2;
private ImageView foreground2;
private ImageView background3;
private ImageView foreground3;
private ImageView crosshair;
public HorizontalScrolling(MainGame game) {
this.game = game;
this.crosshair = game.getCrosshair();
game.setOnMouseMoved(e -> {
crosshair.setTranslateX(e.getX() - background.getFitWidth() / 2);
crosshair.setTranslateY(e.getY() - background.getFitHeight() / 2);
crossPos = e.getX();
});
this.background = game.getBackground();
this.foreground = game.getForeground();
this.background1 = game.getBackgrounds()[0];
this.foreground1 = game.getForegrounds()[0];
this.background2 = game.getBackgrounds()[1];
this.foreground2 = game.getForegrounds()[1];
this.background3 = game.getBackgrounds()[2];
this.foreground3 = game.getForegrounds()[2];
}
public void startScrolling() {
AnimationTimer scrollingTimer = new AnimationTimer() {
@Override
public void handle(long now) {
if (crossPos < 32 * DuckHunt.scale) {
screen = Math.max(screen - 2 * DuckHunt.scale, -game.getBackground().getFitWidth());
} else if (crossPos > 224 * DuckHunt.scale) {
screen = Math.min(screen + 2 * DuckHunt.scale, game.getBackground().getFitWidth());
}
for (Duck duck : game.getDucks()) {
duck.getCurrentImage().setTranslateX(duck.getX() - screen);
}
moveBackground();
}
};
scrollingTimer.start();
}
public void resetBackground() {
screen = 0.0;
}
private void moveBackground() {
background1.setTranslateX(-background.getFitWidth() - screen);
background2.setTranslateX(-screen);
background3.setTranslateX(background.getFitWidth() - screen);
foreground1.setTranslateX(-foreground.getFitWidth() - screen);
foreground2.setTranslateX(-screen);
foreground3.setTranslateX(foreground.getFitWidth() - screen);
}
}