-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra-life.js
52 lines (40 loc) · 1.25 KB
/
extra-life.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
'use strict'
function ExtraLife (canvasElement) {
this.x = (canvasElement.width/2 - 20) + (Math.random()*(50));
this.y = 50;
this.size = 15;
this.speed = 4;
this.direction = 1;
this.canvasElement = canvasElement;
this.ctx = this.canvasElement.getContext('2d');
this.image = new Image();
this.image.src = './images/modern-15-star.svg.png';
}
ExtraLife.prototype.update = function () {
this.checkCollisionWithLimits();
this.y += this.speed * this.direction;
}
ExtraLife.prototype.draw = function () {
this.ctx.beginPath();
this.ctx.arc(this.x,this.y,this.size,0,Math.PI*2,true);
this.ctx.drawImage(this.image, this.x-this.size, this.y-this.size, this.size*2, this.size*2);
// this.ctx.fillStyle = 'green';
// this.ctx.fill();
}
ExtraLife.prototype.setDirection = function (newDirection) {
this.direction = newDirection;
}
// Brick.prototype.setSpeed = function (speedVariation) {
// this.speed += speedVariation;
// }
// Brick.prototype.setLength = function (lengthVariation) {
// this.sizeY += lengthVariation;
// }
ExtraLife.prototype.checkCollisionWithLimits = function () {
if (this.y <= 0 + this.size) {
this.setDirection(1);
}
if (this.y >= this.canvasElement.height - this.size) {
this.setDirection(-1);
}
}