-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoomerang.java
More file actions
76 lines (65 loc) · 1.72 KB
/
Boomerang.java
File metadata and controls
76 lines (65 loc) · 1.72 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
import java.awt.image.BufferedImage;
import java.awt.Graphics;
public class Boomerang extends Sprite {
//Member variables
int speed = 5;
int animationNum = 0;
static BufferedImage[] images;
int maxImageNum = 4; //How many images are in the array above.
int xdirection;
int ydirection;
public Boomerang(){
loadImage();
w = 8;
h = 12;
isActive = true;
}
void cycleImages(){
if (animationNum == maxImageNum - 1){
animationNum = 0;
}
animationNum++;
}
@Override
void Collided(){
isActive = false;
}
//Overridden methods
@Override
void draw(Graphics g){
g.drawImage(images[animationNum], x - View.scrollPositonX, y - View.scrollPositonY, null);
}
//Uses lazy loading to load the pictures
@Override
void loadImage(){
if (images == null){
images = new BufferedImage[maxImageNum];
for (int i = 0; i < maxImageNum; i++){
int tmpNum = i + 1; //To load the images
String tmp = "images/boomerang" + tmpNum + ".png";
images[i] = View.loadImage(tmp);
}
}
}
//Using this method to make the boomerange move in controller
@Override
public boolean update(){
//Moving the boomerang
x += speed * xdirection;
y += speed * ydirection;
cycleImages();
return isActive;
}
@Override
Json Marshal(){
Json ob = Json.newObject();
return ob;
}
@Override
boolean isBoomerang(){return true;}
@Override
public String toString()
{
return "Boomerang (x,y) = (" + x + ", " + y + ")";
}
}