Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple javaScript bug in game - Image not moving vertically
    primarykey
    data
    text
    <p>This game starts after user clicks on first image - which then disappears and the game image (div) appears.</p> <p>I have all the functionality working including score, timer of 30 seconds... But one simple thing is missing which I cannot figure out.</p> <p>Image is supposed to move randomly in any direction but only moves horizontally. I have implemented move method similarly for both horizontal direction by changing style: left property dynamically, but the vertical change is unresponsive when I try to change style: top.</p> <p>Please help. Thanks in advance.</p> <pre><code>&lt;BASE HREF="http://my.campuscruiser.com/cruiser/waldenu/tushar.gerg/game.html"/&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Image click Game!&lt;/title&gt; &lt;script&gt; var score = 0; var left=400; var top = 100; var deltaX=0; var deltaY=0; var width; var height; var interval; function delayedAlert() { window.setTimeout(alert, 30000); } function alert() { clearInterval(interval); var game = document.getElementById('game'); game.style.display= 'none'; var end = document.getElementById('end'); end.style.display = 'block'; document.getElementById("Finalscore").value = score; } function play() { var game = document.getElementById('game'); var firstDiv = document.getElementById('firstDiv'); height = window.innerHeight; width = window.innerWidth; firstDiv.style.display= 'none'; game.style.display= 'block'; newDir(); interval = setInterval("move()", 10); delayedAlert(); } function newDir() { deltaX=Math.floor(Math.random()*11)-5; deltaY=Math.floor(Math.random()*11)-5; } function move() { left= left+deltaX; top = top+deltaY; if (left&lt;=0) { newDir(); deltaX=Math.abs(deltaX); left= left+deltaX; } if (left&gt;=width) { newDir(); deltaX=(-1)*Math.abs(deltaX); left= left+deltaX; } if (top&lt;=0) { newDir(); deltaY=Math.abs(deltaY); top = top+deltaY; } if (top&gt;=height) { newDir(); deltaY=(-1)*Math.abs(deltaY); top = top+deltaY; } var im= document.getElementById('image'); im.style.top=top; im.style.left=left; } function addScore() { score++; document.getElementById("score").value = score; return; } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id ="firstDiv" style="display : block"&gt; &lt;img onClick = "play();" src="pics/playgame.gif"/&gt;&lt;/a&gt; &lt;/div&gt; &lt;div id="game" style="display : none"&gt; &lt;p&gt;Score: &lt;input type= "text" id="score" size="3" value="0"&gt;&lt;/form&gt; Game ends in 30 seconds -- HURRY&lt;/p&gt; &lt;img id="image" src="pics/gameImage.gif" onClick = "addScore();" style="position:relative; left: 400; top: 100; height: 100; width: 80"/&gt; &lt;/div&gt; &lt;div id="end" style ="display: none"&gt; &lt;h1&gt;Game Ended - 30 Seconds up&lt;/h1&gt; &lt;p&gt;Score: &lt;input type= "text" id="Finalscore" size="3"&gt;&lt;/form&gt; &lt;/p&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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