-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.as
124 lines (115 loc) · 2.37 KB
/
Enemy.as
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
package
{
import flash.display.MovieClip;
public class Enemy extends Target
{
public var counter:int = 0;
public var ySpeed:int = 2;
public var xSpeed:int = 2;
public var speed:int = 1;
public var dirNum:int = 0;
public var yPos:int;
public var xPos:int;
public function Enemy(main:Main, xPos:int, yPos:int)
{
this.main = main;
x = xPos;
x = yPos;
this.xPos = xPos;
this.yPos = yPos;
mySound = Sounds.pingEnemy;
objectName = "Enemy";
}
public override function update():void
{
super.update();
if (counter % 15 == 0)
{
var randomAction = Math.floor(Math.random() * 3); //helpds decide on a new random direction
switch (randomAction)
{
case 0 :
dirNum++;
break;
case 1 :
dirNum--;
break;
default :
break;
}
if (dirNum > 7) //ensures that the movement is smooth, ie no sudden changes in directions
{
dirNum = 0;
}
if (dirNum == 0)
{
gotoAndStop("downRight");
xSpeed = speed;
ySpeed = speed;
}
else if (dirNum == 1)
{
gotoAndStop("down");
xSpeed = 0;
ySpeed = speed;
}
else if (dirNum == 2)
{
gotoAndStop("downLeft");
xSpeed = - speed;
ySpeed = speed;
}
else if (dirNum == 3)
{
gotoAndStop("left");
xSpeed = - speed;
ySpeed = 0;
}
else if (dirNum == 4)
{
gotoAndStop("upLeft");
xSpeed = - speed;
ySpeed = - speed;
}
else if (dirNum == 5)
{
gotoAndStop("up");
xSpeed = 0;
ySpeed = - speed;
}
else if (dirNum == 6)
{
gotoAndStop("upRight");
xSpeed = speed;
ySpeed = - speed;
}
else if (dirNum == 7)
{
gotoAndStop("right");
xSpeed = speed;
ySpeed = 0;
}
//make sure the enemy subs only travels within a constricted predetermined area
if ((this.x + xSpeed) <= (xPos-100))
{
xSpeed = speed;
}
else if ((this.x + xSpeed) >= (xPos+100))
{
xSpeed = - speed;
}
if ((this.y + ySpeed) <= (yPos-100))
{
ySpeed = speed;
}
else if ((this.y + ySpeed) >= (yPos+100))
{
ySpeed = - speed;
}
}
this.x += xSpeed;
this.y += ySpeed;
counter++;
}
}
}