Moving image across the screen

javascript

Solution

Try to move

var init=0;
var x = 0;
var dest_x = 800;
var interval = 10;

before `function Run(){`.

Now you overwrite results with initial data.

Problem

Am a beginner in html5 and am facing this problem in a simple program to move an image across the screen. Its a simple pacman program where i used two images. One is a pacman with mouth open and other with mouth closed When i tried to mov it across the screen towards right and then it has to return to its initial position. It tried many ways but none works. It moves only once for a step and for the next step it is static. It will be really helpful if anyone could solve this ``` <html> <head> <SCRIPT> var timer = setInterval(Run,500); flag = 1; function Run(){ img1 = document.getElementById("PacMan"); var init=0; var x = 0; var dest_x = 800; var interval = 10; if(x<dest_x) x = x + interval; img1.style.left = x+"px"; if (x+interval < dest_x) img1.style.left = init+"px"; if(flag ==1){ img1.src = "PacMan2.png"; flag=0; } else { img1.src = "PacMan1.png"; flag=1; } } </SCRIPT> </head> <body> <img id="PacMan" src = "PacMan1.png" onClick=clearInterval(timer) style="position:absolute"> </img> </body> </html> ```

Original source