Johan Westborg Johan Westborg - 6 months ago 12
jQuery Question

Javascript, print out message when all images are clicked

i'm having a slight problem here with my javascript code. I'm making a game for a school project and it's about clicking(removing the fish) on fish with a fishing rod. But right now you can't win the game since i haven't figured out how to make the game stop when all the fishes are clicked(removed). Under here is my entire html code and javascript code if you wanna have a look. There is also a jsbin link where you can see the game in action with all the css and html and javascript together so it's easier for you to see what i mean.
Do anybody know how to make the game stop when all the fishes are clicked(removed) and make an alert saying "You Won!".

Thanks

https://jsbin.com/fihebiwiqi/edit?html,css,output

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>

<link rel="stylesheet" type="text/css" href="css1.css">
</head>

<body background="https://guideinparadise.files.wordpress.com/2013/01/down-below2.jpg">
<h1>Fiskaren</h1>
<img id="fiskespo" src ="https://pixabay.com/static/uploads/photo/2014/03/24/17/07/fishing-rod-295096_960_720.png">
<p>Sekunder:</p>
<p id="klocka"></p>
<form action="html.html">
<input id='level1' type="submit" value="Level 1">
</form>
<form action="html2.html">
<input id='level2' type="submit" value="Level 2">
</form>
<form action="html3.html">
<input id='level3' type="submit" value="Level 3">
</form>
<form action="html4.html">
<input id='level4' type="submit" value="Level 4">
</form>
<form action="html5.html">
<input id='level5' type="submit" value="Bonus bana">
</form>


<script>
document.body.style.cursor = 'none'; // tar bort muspekaren
$(document).mousemove(function (e) { //gör en funktion för musen
$('#fiskespo').offset({ // tar fram bilden
left: e.pageX + -190, //positonerar musen på bilden
top: e.pageY + -110//positonerar musen på bilden
});
});

var b = 1;
for (var i= 0; i<5;i++){ // skapar en loop som skriver ut 5 stycken bilder på fiskar
fisk(b);// länkar till funktionen fisk där bilden som ska ligga i loopen finns.
}
function positionering(element) { //skapar en funktion som ska göra allt random
var x = document.body.offsetHeight-element.clientHeight;
var y = document.body.offsetWidth-element.clientWidth;
var Xled = Math.floor(Math.random()*1*x + 300);
var Yled = Math.floor(Math.random()*y + 50);

return [Xled,Yled]; //stoppar funktionen och positionerar bilderna med hjälp av funktionen randomX och randomY


}

function fisk() { //skapar en funktion

var img = document.createElement('img'); //skapar ett elememt som heter img
img.className = 'fisk2';
img.setAttribute("style", "position:fixed;");//positionerar dom
img.setAttribute("src", "http://res.freestockphotos.biz/pictures/16/16806-illustration-of-a-cartoon-blue-fish-pv.png"); // tar fram bilderna
img.setAttribute("width", "200");// bestämmer bredden på bilden
document.body.appendChild(img);// bifogar body till img
var xy = positionering(img); // ger xy random position
img.style.top = xy[0] + 'px'; //positionerar bilderna
img.style.left = xy[1] + 'px'; //positionerar bilderna
$(img).click(function(){ $(this).remove();}); //skapar en onclick funktion som gör så att om man klickar på bilden försvinner den

}
myTimer = setInterval(myCounter, 1000); //bestämmer hur lång tiden ska vara (1000 = 1 vanlig sekund)
var seconds = 0; //sekunden den börjar på
function myCounter() {
document.getElementById("klocka").innerHTML = ++seconds; //räknar klockan i sekunder
}
</script>

</body>
</html>

Answer

You have fisk(b); but you never use that argument...
myTimer misses a var and should be defined at the code-top.

Create a var

var fishes = 5;

use that var in your for loop (instead of 5)
Than edit this line $(img).click(function(){ $(this).remove();}); to be:

$(img).click(function(){

     fishes -= 1;
     $(this).remove();

     if(!fishes) {
        clearInterval(myTimer);
        alert("It took you "+ seconds +" seconds.");
     }

});
Comments