-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdom.js
More file actions
70 lines (62 loc) · 2.17 KB
/
dom.js
File metadata and controls
70 lines (62 loc) · 2.17 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
57
58
59
60
61
62
63
64
65
66
var assetsPath = 'assets/';
var bounceCycle = [
'tile050.png', 'tile051.png', 'tile052.png', 'tile053.png', 'tile054.png',
'tile055.png', 'tile056.png', 'tile057.png',
];
var jumpingRollCycle = [
'tile050.png', 'tile051.png', 'tile052.png', 'tile053.png', 'tile054.png',
'tile055.png', 'tile056.png', 'tile057.png','tile058.png', 'tile059.png',
'tile060.png', 'tile061.png', 'tile062.png', 'tile063.png', 'tile064.png',
'tile065.png', 'tile066.png', 'tile067.png', 'tile068.png', 'tile069.png',
'tile070.png', 'tile071.png', 'tile072.png', 'tile073.png', 'tile074.png',
'tile075.png', 'tile076.png', 'tile077.png', 'tile078.png', 'tile079.png',
]
var slime = document.getElementById("slime");
var slimeAnimation;
//SLIME --- starting point, for image, BOUNCE ANIMATION
document.addEventListener('DOMContentLoaded', function () {
bounceAnimation();
var gameTimer;
document.getElementById('startGameBtn').addEventListener('click', function () {
if (gameTimer) {
clearInterval(gameTimer); //to stop timer
gameTimer = undefined;
bounceAnimation();
return;
}
var offset = 0;
gameTimer = setInterval(function () {
document.getElementsByTagName('body')[0].style.backgroundPosition = 'left ' + offset + 'px top 180px';
offset -= 3;
}, 70);
jumpRollAnimation();
});
}, false);
function bounceAnimation() {
if (slimeAnimation) {
clearInterval(slimeAnimation);
}
var bounceIndex = 0;
slimeAnimation = setInterval(function () {
var frame = assetsPath + bounceCycle[bounceIndex]; //set the path
slime.src = frame; //connect pwd to img id="slime"
bounceIndex++;
if (bounceIndex === bounceCycle.length) {
bounceIndex = 0;
}
}, 120);
}
function jumpRollAnimation() {
if (slimeAnimation) {
clearInterval(slimeAnimation);
}
let index = 0;
slimeAnimation = setInterval(function () {
var jumpRollCyc = assetsPath + jumpingRollCycle[index]; //set the path
slime.src = jumpRollCyc; //connect pwd to img id="slime"
index++;
if (index === jumpingRollCycle.length) {
index = 0;
}
}, 120);
}