-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBomb.js
More file actions
31 lines (25 loc) · 930 Bytes
/
Bomb.js
File metadata and controls
31 lines (25 loc) · 930 Bytes
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
function Bomb(lifeTime) {
this.lifeTime = lifeTime;
this.die = function() {
this.element.dispatchEvent(new Event('died'));
this.element.parentNode.removeChild(this.element);
}
this.explode = function() {
this.element.classList.add('exploded');
this.element.dispatchEvent(new Event('exploded'));
}
this.tick = function () {
this.lifeTime--;
this.element.textContent = this.lifeTime;
if (this.lifeTime <= 0) {
this.explode();
}
}
this.element = document.createElement('button');
this.element.classList.add('bomb');
this.element.textContent = this.lifeTime;
this.element.tick = this.tick.bind(this);
this.element.addEventListener('click', this.die.bind(this));
this.element.style.left = Math.trunc(Math.random() * 100) + "%";
this.element.style.top = Math.trunc(Math.random() * 100) + "%";
}