admin管理员组文章数量:1173636
I'm writing a code to move a character in a browser game. I managed to get the pixels it must move per second both horizontally and vertically.
pxsecx is the number of pixels it must move horizontally per second pxsecy is the same but vertically
Basically it should += them to the current horizontal and vertical position.
I need the loop to keep repeating itself every second until the element position meets the new position (newx).
This is as far as I have gone:
<body onmousedown="showCoords(event)">
<script type="text/javascript">
function showCoords(evt){
oldx = parseInt(document.getElementById("character").style.left);
oldy = parseInt(document.getElementById("character").style.top);
width = parseInt(document.getElementById("character").style.width);
height = parseInt(document.getElementById("character").style.height);
newx = evt.pageX - width/2;
newy = evt.pageY - height/2;
disx = newx - oldx;
disy = newy - oldy;
diag = parseInt(Math.sqrt(disx*disx + disy*disy));
speed = 50;
secs = diag/speed;
pxsecx = disx/secs;
pxsecy = disy/secs;
while(document.getElementById("character").style.left<newx)
{
document.getElementById("character").style.left += pxsecx;
document.getElementById("character").style.top += pxsecy;
}
}
</script>
Everything works until the while where I have no idea how to do make it work every second. I'm testing it here: .php
How do I make it repeat that once a second so it works?
thanks
I'm writing a code to move a character in a browser game. I managed to get the pixels it must move per second both horizontally and vertically.
pxsecx is the number of pixels it must move horizontally per second pxsecy is the same but vertically
Basically it should += them to the current horizontal and vertical position.
I need the loop to keep repeating itself every second until the element position meets the new position (newx).
This is as far as I have gone:
<body onmousedown="showCoords(event)">
<script type="text/javascript">
function showCoords(evt){
oldx = parseInt(document.getElementById("character").style.left);
oldy = parseInt(document.getElementById("character").style.top);
width = parseInt(document.getElementById("character").style.width);
height = parseInt(document.getElementById("character").style.height);
newx = evt.pageX - width/2;
newy = evt.pageY - height/2;
disx = newx - oldx;
disy = newy - oldy;
diag = parseInt(Math.sqrt(disx*disx + disy*disy));
speed = 50;
secs = diag/speed;
pxsecx = disx/secs;
pxsecy = disy/secs;
while(document.getElementById("character").style.left<newx)
{
document.getElementById("character").style.left += pxsecx;
document.getElementById("character").style.top += pxsecy;
}
}
</script>
Everything works until the while where I have no idea how to do make it work every second. I'm testing it here: http://chusmix.com/game/movechar.php
How do I make it repeat that once a second so it works?
thanks
Share Improve this question edited May 3, 2011 at 4:52 lisovaccaro asked May 3, 2011 at 4:41 lisovaccarolisovaccaro 33.9k99 gold badges269 silver badges423 bronze badges5 Answers
Reset to default 16JavaScript is primarily asynchronous, so you'll need to rewrite this a little. setTimeout
executes a function after a certain amount of time. Therefore, you can do this:
(function move() {
var character=document.getElementById("character");
if(character.style.left<newx) {
character.style.left += pxsecx;
character.style.top += pxsecy;
setTimeout(move, 1000);
}
})();
You can use the function setInterval(function, interval)
// To start the loop
var mainLoopId = setInterval(function(){
// Do your update stuff...
move();
}, 40);
// To stop the loop
clearInterval(mainLoopId);`
to loop something every second you'll have to use a set interval:
let before = new Date();
setInterval(() => {
console.log(Math.round((new Date() - before) / 1000));
}, 1000);
this code gets the date of the run and is subtracted to the date of every second. (by the way it starts at one not at zero, so you will have to do it manually if you want)
I made a coffescript class to handle time loops, maybe it will be helpful for someone.
# Classe TimeLoop, execute a function every x miliseconds
#
# @example How o create a loop
# myLoop = new TimeLoop((-> alert("loop"),1000)
# myLoop.start()
#
class window.TimeLoop
constructor: (@function,@miliseconds) ->
# Start loop.
#
start: -> @loop = setInterval @function, @miliseconds
# Stop loop.
#
stop: -> clearInterval(@loop)
link to gist: https://gist.github.com/germanotm/6ee68f804860e2e77df0
You want the JavaScript setTimeout()
function. It calls a function every n milliseconds.
本文标签: Simple Javascript loop that repeats each secondStack Overflow
版权声明:本文标题:Simple Javascript loop that repeats each second - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737988198a2045107.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论