Johan Westborg Johan Westborg - 2 years ago 67
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!".


<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src=""></script>

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

<body background="">
<img id="fiskespo" src ="">
<p id="klocka"></p>
<form action="html.html">
<input id='level1' type="submit" value="Level 1">
<form action="html2.html">
<input id='level2' type="submit" value="Level 2">
<form action="html3.html">
<input id='level3' type="submit" value="Level 3">
<form action="html4.html">
<input id='level4' type="submit" value="Level 4">
<form action="html5.html">
<input id='level5' type="submit" value="Bonus bana">

<script> = '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", ""); // 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 = xy[0] + 'px'; //positionerar bilderna = 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


Answer Source

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:


     fishes -= 1;

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download