-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFallingObject.java
66 lines (60 loc) · 1.91 KB
/
FallingObject.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class fallingObject here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class FallingObject extends animatedObject
{
/**
* Act - do whatever the fallingObject wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public double vSpeed = 0;
public double hSpeed = 0;
public double acceleration = 1;
public long deltaTime;
public long lastFrame = System.nanoTime();
public long currentFrame = System.nanoTime();
public long fallTime;
public int rotate;
public FallingObject(double vSpeed, double acceleration, double hSpeed, int rotate, long time) {
this.vSpeed = vSpeed;
this.acceleration = acceleration;
this.rotate = rotate;
this.hSpeed = hSpeed;
this.fallTime = time;
lastFrame = System.nanoTime();
currentFrame = System.nanoTime();
}
public void act()
{
update();
}
public void update() {
currentFrame = System.nanoTime();
deltaTime = (currentFrame - lastFrame) / 1000000;
if (deltaTime < fallTime) {
double x = getExactX()+hSpeed;
double y = getExactY()+vSpeed;
setLocation(x,y);
turn(rotate);
vSpeed = vSpeed + acceleration;
} else {
checkDeath();
}
}
public void checkDeath() {
if (getImage().getTransparency() > 0) {
if (getImage().getTransparency()-3 <= 0) {
getImage().setTransparency(0);
} else {
getImage().setTransparency(getImage().getTransparency()-3);
}
} else {
getWorld().removeObject(this);
return;
}
}
}