-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSprite.java
175 lines (94 loc) · 2.04 KB
/
Sprite.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public abstract class Sprite
{
private boolean loop;
private Image[] imageList;
private Image image;
private int _totalframes = 0; private int _currentframe = 0; private int _height; private int _width; private int _y = 0; private int _x = 0;
public Sprite(Image i, BreakEngine e)
{
image = i;
_width = image.getWidth();
_height = image.getHeight();
}
public Sprite(Image[] i, boolean l, BreakEngine e)
{
imageList = i;
loop = l;
image = imageList[0];
_width = image.getWidth();
_height = image.getHeight();
_totalframes = (imageList.length - 1);
}
public int getX()
{
return _x;
}
public int getY()
{
return _y;
}
public void setX(int x)
{
_x = x;
}
public void setY(int y)
{
_y = y;
}
public void setPos(int x, int y)
{
_x = x;
_y = y;
}
public int getWidth()
{
return _width;
}
public int getHeight()
{
return _height;
}
public void nextFrame()
{
if (_currentframe < _totalframes) {
_currentframe += 1;
}
else if (loop) { _currentframe = 0;
}
image = imageList[_currentframe];
}
public void prevFrame()
{
if (_currentframe >= 0) {
_currentframe -= 1;
}
else if (loop) { _currentframe = _totalframes;
}
image = imageList[_currentframe];
}
public void setFrame(int i)
{
if (i < 0) { i = 0;
} else if (i > _totalframes) { i = _totalframes;
}
_currentframe = i;
}
public Image getImage()
{
return image;
}
public boolean collidesWith(Sprite sprite)
{
int bX = sprite.getX();
int bY = sprite.getY();
int bWidth = sprite.getWidth();
int bHeight = sprite.getHeight();
return (_x > bX - _width) && (_x < bX + bWidth) && (_y > bY - _height) && (_y < bY + bHeight);
}
public void draw(Graphics g)
{
g.drawImage(image, _x, _y, 20);
}
}