-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ghost2.java
64 lines (59 loc) · 2.02 KB
/
Ghost2.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
import greenfoot.*;
public class Ghost2 extends Actor
{
private int speed;
private boolean movingRight;
private int stopPointLeft;
private int stopPointRight;
public Ghost2(int speed, int stopPointLeft, int stopPointRight) {
this.speed = speed;
this.movingRight = true; // default ke kanan
this.stopPointLeft = stopPointLeft;
this.stopPointRight = stopPointRight;
}
public void act()
{
if (getWorld() == null) {
return; // Jika objek telah dihapus dari dunia, hentikan eksekusi
}
checkCollision();
if (getWorld() == null) {
return; // Jika objek telah dihapus dari dunia, hentikan eksekusi
}
moveHorizontally();
}
public void moveHorizontally() {
if (getWorld() == null) {
return; // Jika objek telah dihapus dari dunia, hentikan eksekusi
}
checkCollision();
if (getWorld() == null) {
return; // Jika objek telah dihapus dari dunia, hentikan eksekusi
}
if (movingRight) {
move(speed);
if (isAtEdge() || isTouching(Platform2.class) || getX() >= stopPointRight) {
movingRight = false;
setImage("Ghost2-Walk-Left.png");
}
} else {
move(-speed);
if (isAtEdge() || isTouching(Platform2.class) || getX() <= stopPointLeft) {
movingRight = true;
setImage("Ghost2-Walk-Right.png");
}
}
}
private void checkCollision() {
skill1Charge skill1 = (skill1Charge) getOneIntersectingObject(skill1Charge.class);
skill2Charge skill2 = (skill2Charge) getOneIntersectingObject(skill2Charge.class);
if (skill1 != null) {
Greenfoot.playSound("Magic-Explode.mp3");
getWorld().removeObject(this);
}
if (skill2 != null) {
Greenfoot.playSound("Magic-Explode.mp3");
getWorld().removeObject(this);
}
}
}