Interval = time between repeats
1000 = once per second
9. Movement
let x = 0;
let loop;
function move(){
x += 5;
if(x > 400){
clearInterval(loop);
}
moveBox.style.marginLeft = x + "px";
}
function startMove(){
clearInterval(loop);
loop = setInterval(move, 50);
}
function stopMove(){
clearInterval(loop);
}
function reset(){
x = 0;
moveBox.style.marginLeft = "0px";
}
10. Sprite Animation
let frame = 0;
function animate(){
frame++;
if(frame > 5){
frame = 0;
}
player.src = "animations/man-walking/" + frame + ".png";
}
function startAnim(){
loop = setInterval(animate, 100);
}
function stopAnim(){
clearInterval(loop);
}