0
var b = 1; 
for(var i= 0; i < 5; i++){ 
    fisk(b);
}

function getRandomPosition(element) {
    var x = document.body.offsetHeight-element.clientHeight;
    var y = document.body.offsetWidth-element.clientWidth;
    var randomX = Math.floor(Math.random() * x);
    var randomY = Math.floor(Math.random() * y);

    return [randomX,randomY];
}

function fisk(skala) { 
    var img = document.createElement('img');
    img.setAttribute("style", "position:fixed;");
    img.setAttribute("src", "http://i.imgur.com/K9egEbW.jpg"); 
    document.body.appendChild(img); 
    var xy = getRandomPosition(img); 
    img.style.top = xy[0] + 'px';
    img.style.left = xy[1] + 'px';
}

With this code above i have made a tiny game where i am supposed to click on some fished that spawns with this code and when you click them all you win. But i'm having trouble finding out how to make the images disappear when i click them. Here is a jsbin link that shows all i've done so far so it's easier to understand. https://jsbin.com/josamuxoce/edit?html,css,js Anybody have a clue on how to make the fishes that spawn disappear when i click on them with the fishing rod?

Thanks

  • 1
    Start by fixing the console errors in the jsbin link and your code above, by removing the stray '1' on line 6 of the fisk function. Also, is there supposed to be any event handlers already for clicking fish? – ghost_dad May 27 '16 at 17:42

1 Answers1

-1

Add $(img).click(function(){ $(this).remove();}); to function fisk(skala); But. img will never get click becaurse all clicks will be handled by #fiskespo.

Use fiskespo image as cursor image instead of extended img object (Using external images for CSS custom cursors). Should work;

Community
  • 1
  • 1