Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple javascript html image game
    primarykey
    data
    text
    <p>I have a simple game where image moves randomly and score increases when user clicks on it.</p> <p>The first image displays before game is started, which when clicked calls the play() function in javascript, which hides that image and displays the image that is to be used for the game.</p> <p>That is where my code is stuck, it does not call the function play(). I am new to javascript and html. Any help would be great!</p> <p>Here is my code</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Image click Game!&lt;/title&gt; &lt;script&gt; global var score = 0; global var left=400; global var top = 100; function play() { var game = document.getElementById('game'); var firstDiv = document.getElementById('firstDiv'); var height = window.innerHeight; var width = window.innerWidth; firstDiv.style = 'display : none'; game.style='display : block'; setInterval("move()", 1000); } function move() { var randomNumberX = Math.floor(Math.random()*11)-5; var randomNumberY = Math.floor(Math.random()*11)-5; left = left + randomNumberX; top = top+randomNumberY; var im = document.getElementById('image'); im.style.left = left; im.style.top = top; } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id ="firstDiv" style="display : block"&gt; &lt;img src="pics/playgame.gif" width="108" height="106" onClick = "play()"/&gt;&lt;/a&gt; &lt;/div&gt; &lt;div id="game" style="display : none"&gt; &lt;p&gt;"Score: " + score&lt;/p&gt; &lt;img id="image" src="pics/gameImage.gif" onClick = "score++" style="position:absolute; left: 400; top: 100;"/&gt;&lt;/a&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload