-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtree.js
More file actions
56 lines (44 loc) · 1.21 KB
/
tree.js
File metadata and controls
56 lines (44 loc) · 1.21 KB
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
function Tree() {
this.width = 80;
this.height = 80;
this.x;
this.y;
this.isOnScreen;
this.side;
var self = this;
var image = new Image();
this.initialize = function (initialPosition, sidePar) {
this.side = sidePar;
image.src = this.getRandomTreeImageSrc();
this.x = this.newXPosition();
this.y = (-60 - this.height) * initialPosition;
this.isOnScreen = true;
};
this.draw = function (context) {
context.drawImage(image, this.x, this.y, this.width, this.height);
};
this.update = function (maxY) {
this.y += 1;
if (this.y >= maxY) {
this.isOnScreen = false;
this.y = -this.height;
this.x = this.newXPosition();
} else {
this.isOnScreen = true;
}
};
this.newXPosition = function () {
if (this.side == 0) {
return Math.random() * (170 - this.width);
} else {
return 650 + Math.random() * (160 - this.width);
}
};
this.getRandomTreeImageSrc = function () {
var imageSrcArray = [];
imageSrcArray[0] = "sprites/scenario/tree1.png";
imageSrcArray[1] = "sprites/scenario/tree2.png";
var index = Util.getRandomIntBetweenInterval(0, imageSrcArray.length - 1);
return imageSrcArray[index];
};
}