-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.js
58 lines (53 loc) · 1023 Bytes
/
enemy.js
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
Enemy = function (x, y)
{
this.x = x;
this.y = y;
this.width = 30;
this.height = 22;
this.hasteX = 1;
this.hasteY = 1;
this.valueScore = 10;
this.image = new Image();
this.image.src = "images/invaders1.gif";
}
Enemy.prototype.draw = function (context)
{
context.drawImage(this.image, 0, 0, this.width, this.height, this.x, this.y, this.width, this.height);
}
Enemy.prototype.moveDown = function (interval, enemyRef)
{
setInterval(function(){
enemyRef.y = enemyRef.y + enemyRef.hasteY;
},
interval
);
}
Enemy.prototype.moveHorizontal = function (interval, enemyRef, minWidth, maxWidth)
{
var add = true;
setInterval(function(){
if(enemyRef.x < minWidth)
{
add = true;
}
else if (enemyRef.x > maxWidth)
{
add = false;
}
if(add)
{
enemyRef.x = enemyRef.x + enemyRef.hasteX;
}
else
{
enemyRef.x = enemyRef.x - enemyRef.hasteX;
}
},
interval
);
}
Enemy.prototype.randomPos = function(sceneRef)
{
var x = randomNum(0, (sceneRef.width - this.width));
this.x = x;
}