-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlevel_3.js
83 lines (51 loc) · 2.25 KB
/
level_3.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// global variable to store the score
var score = 0;
function createImage () {
var $guy;
// new image is created
// 20% chance of displaying "good", 80% of displaying "bad"
if (Math.random() < 0.2) {
$guy = $('<img class="good" src="ext/homer.png" />');
} else {
$guy = $('<img class="bad" src="ext/burns.png" />');
}
// random positionning. It uses the fact that in the css file we set the images with "position: absolute"
$guy.css('left', Math.random()*500+'px');
$guy.css('top', Math.random()*300+'px');
// the image is added to the scene
$('body').append($guy);
// in jQuery, you can chain instructions. Here we hide the image to be able to make it appear smoothly in a fade
$guy.hide().fadeIn();
// after 2 seconds, the image disappears
setTimeout(function () {
$guy.fadeOut();
}, 2000);
}
// this function will call itself after a randomized delay, in order to make images appear one after the other
function gameLoop () {
createImage();
setTimeout(gameLoop, 200 + Math.random()*500);
}
$(function () {
/* Detection of the click on an image.
You would probably use a syntax like :
$('img').click(function () { ...
but this one would not work here : it means "Take all img elements CURRENTLY present on the page, and when the user clicks on them, trigger the function"
Here, when this instruction is executed, no image has been added to the page yet. The images will be added later.
The trick is to use this syntax :
$('body').on('click', 'img', function () {
which means : "whenever an img inside body is clicked, trigger the function"
It applies to all past and future img in the body
*/
$('body').on('click', 'img', function () {
$(this).remove(); // the element is completely removed from the html
if ($(this).hasClass('good')) {
score -= 3; // penalty if the user hits a "good" image
} else {
score += 1;
}
$('#score').html(score);
});
// starts the image apparition process
gameLoop();
});