-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnemyShip.as
104 lines (99 loc) · 2.23 KB
/
EnemyShip.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
package {
import flash.display.MovieClip
import flash.events.Event;
import flash.utils.Timer;
public class EnemyShip extends MovieClip {
var speed:Number;
static var list:Array = new Array();
var shootTimer:Timer;
var health:Number;
var yDirection:Number;
function EnemyShip() {
list.push(this);
this.x = 700;
this.y = Math.random()*200+50;
speed = Math.random()*5+5;
var yrand = Math.random()*3;
if (yrand > 2) {
yDirection = (yrand - .5) * 3;
}
addEventListener("enterFrame", enterFrame);
var interval:Number = Math.random()*500+1000;
shootTimer = new Timer(interval);
shootTimer.addEventListener("timer", shoot);
shootTimer.start();
health = 1;
}
function takeDamage(d){
health -= d;
if(health <= 0) {
kill();
}
}
function enterFrame(e:Event){
this.x -= speed;
if(this.x < -100){
destroy();
return;
}
if(yDirection)
{
this.y += yDirection;
if(yDirection > 0 && this.y > 275)
{
yDirection *= -1;
}
else if(yDirection < 0 && this.y < 25)
{
yDirection *= -1;
}
}
if(this.hitTestObject(Game.ship)){
takeDamage(3);
if(Game.ship.shield.visible == false){
Game.ship.takeDamage(20);
}
}
}
function kill() { //destroy and give rewards
if(yDirection) {
reward(15);
} else {
reward(5);
}
var s = new ExplosionSound();
s.play();
var explosion = new Explosion();
stage.addChild(explosion);
explosion.x = this.x;
explosion.y = this.y;
destroy();
Game.kills += 1;
}
function destroy() {
shootTimer.stop();
shootTimer.removeEventListener("timer",shoot);
removeEventListener("enterFrame", enterFrame);
for(var i in list){
if(list[i] == this){
delete list[i];
}
}
stage.removeChild(this);
}
function reward(points:int) {
var pd = new PointDisplay();
pd.displayText.text = points.toString();
pd.x = this.x;
pd.y = this.y;
stage.addChild(pd);
Game.updateScore(points);
}
function shoot(e:Event){
var b = new EnemyBullet();
b.x = this.x - 50;
b.y = this.y;
stage.addChild(b);
}
}
}