-
Notifications
You must be signed in to change notification settings - Fork 4
/
game.js
37 lines (32 loc) · 888 Bytes
/
game.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
const sceneEl = document.querySelector("a-scene")
const heartEl = document.querySelector("#heart-model")
const scoreEl = document.querySelector('#score-element');
function randomPosition() {
return {
x: (Math.random() - 0.5) * 20,
y: 1.5,
z: (Math.random() - 0.5) * 20
};
}
let score = 0;
function displayScore() {
scoreEl.setAttribute('value', `Score: ${score}`);
}
function createHeart(){
const clone = heartEl.cloneNode()
clone.setAttribute("position", randomPosition())
clone.addEventListener('mousedown', () => {
score++;
clone.dispatchEvent(new Event('collected'));
displayScore();
})
clone.addEventListener('animationcomplete', () => {
clone.setAttribute("position", randomPosition());
clone.setAttribute('scale', '0.01 0.01 0.01');
});
sceneEl.appendChild(clone)
}
for(let i=0 ; i<15; i++){
createHeart()
}
displayScore()