-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlyingSprite.java
More file actions
97 lines (90 loc) · 2.47 KB
/
FlyingSprite.java
File metadata and controls
97 lines (90 loc) · 2.47 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// FlyingSprite: each object represents an animated "flying" image in
// the "game"
//
//import javafx.event.Event;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.application.Platform;
import javafx.scene.layout.Pane;
import javafx.geometry.Rectangle2D;
public class FlyingSprite extends ImageView
{
private int xDirection = 1;
private int yDirection = 1;
private int dirRate = 2;
private Pane area;
private Image sprite;
private int width;
private int height;
private Rectangle2D spriteRects[];
private int startSpriteX, startSpriteY;
private int widthSpriteX, heightSpriteY;
private int curSpriteIndex;
private int spriteSpeed;
public FlyingSprite(Pane gamePane, int x, int y, int w, int h, String spriteFile)
{
sprite = new Image(spriteFile);
setImage(sprite);
setFitWidth(w);
setPreserveRatio(true);
setSmooth(true);
setCache(true);
area = gamePane;
width = w;
height = h;
Platform.runLater(() -> {
setX(x);
setY(y);
});
}
public FlyingSprite(Pane gamePane, int x, int y, int w, int h, String spriteFile, int sx, int sy, int ws, int hs, int num)
{
sprite = new Image(spriteFile);
setImage(sprite);
setSmooth(true);
area = gamePane;
width = w;
height = h;
startSpriteX = sx;
startSpriteY = sy;
widthSpriteX = ws;
heightSpriteY = hs;
spriteSpeed = 0;
spriteRects = new Rectangle2D[num];
for (int i=0; i < num; i++) {
spriteRects[i] = new Rectangle2D(startSpriteX+(i*widthSpriteX), startSpriteY, widthSpriteX, heightSpriteY);
}
curSpriteIndex = 0;
setViewport(spriteRects[curSpriteIndex]);
Platform.runLater(() -> {
setX(x);
setY(y);
});
}
// update the box's position; x,y is upper left corner on a box
// so we need to add its size when checking the right and bottom
// bounds
public void move()
{
if (spriteRects != null) {
if (spriteSpeed == 4) {
curSpriteIndex++;
if (curSpriteIndex >= spriteRects.length)
curSpriteIndex = 0;
setViewport(spriteRects[curSpriteIndex]);
}
spriteSpeed = (spriteSpeed+1)%5;
}
if (getX() + width > area.getWidth() || getX() < 0) {
xDirection = -xDirection;
}
if (getY() + height > area.getHeight() || getY() < 0) {
yDirection = -yDirection;
}
Platform.runLater(() -> {
setX(getX() + dirRate * xDirection);
setY(getY() + dirRate * yDirection);
});
}
}