admin管理员组文章数量:1429729
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.
本文标签: javascriptMoving image across the screenStack Overflow
版权声明:本文标题:javascript - Moving image across the screen - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745550157a2662904.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论