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>
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>
Share
Improve this question
edited Mar 25, 2013 at 11:27
Rob
15.2k30 gold badges48 silver badges73 bronze badges
asked Mar 25, 2013 at 10:52
PreethiPreethi
651 silver badge6 bronze badges
1
- 2 In x + interval you add interval always to zero; First get current div position. – Damian0o Commented Mar 25, 2013 at 11:06
4 Answers
Reset to default 4Try 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.
var timer = setInterval("Run()",500);
you missed some mas!
Try to read current pacMan position with
var x = img1.offsetLeft;
In my opinion, you have to make this using Canvas (view the example on MDN) and JavaScript.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745550157a4632538.html
评论列表(0条)