开发者

How would I make these animations animate at different times?

开发者 https://www.devze.com 2023-03-17 00:50 出处:网络
I\'m making a whack-a-mole game and this is what I have so far, I\'m aware this is sloppy and probably isn\'t the easiest/smartest way to do this. What I need to know is how to get my animations to an

I'm making a whack-a-mole game and this is what I have so far, I'm aware this is sloppy and probably isn't the easiest/smartest way to do this. What I need to know is how to get my animations to animate at separate times. This current code works to animate 9 different moles coming out of holes but I need them to animate at different times(so none of them will come up at the same time, or within a few milliseconds of eachother) My current code is

<html>
<body>
<style type="text/css">
body, a, a:hover {cursor: url(http://cur.cursors-4u.net/others/oth-5/oth438.cur),
progress;}
</style>
<body background = "http://i52.tinypic.com/34e9ekj.jpg">
<b><center><font size="5"><div id='counter'>0</div></font></center><b>
<b><center><i>Whack-A-Mole</i> - by Steven</center></b>
<div
   style="
      top: 37;
      left: 350;
      position: absolute;
      z-index: 1;
      visibility: show;">
<center><img id='animation0' src ='http://i51.tinypic.com/sxheeo.gif'/></center>
</div>
<div
   style="
      top: 37;
      left: 33;
      position: absolute;
      z-index: 1;
      visibility: show;">
<left><img id='animation1' src ='http://i51.tinypic.com/sxheeo.gif'/></left>
</div>

<div
   style="
      top: 37;
      left: 700;
      position: absolute;
      z-index: 1;
      visibility: show;">
<right><img id='animation2' src ='http://i51.tinypic.com/sxheeo.gif'/></right>
</div>
<div
   style="
      top: 200;
      left: 352;
      position: absolute;
      z-index: 1;
      visibility: show;">
<img id='animation3' src ='http://i51.tinypic.com/sxheeo.gif'/>
</div>
<div
   style="
      top: 200;
      left: 33;
      position: absolute;
      z-index: 1;
      visibility: show;">
<img id='animation4' src ='http://i51.tinypic.com/sxheeo.gif'/>
</div>
<div
   style="
      top: 200;
      left: 700;
      position: absolute;
      z-index: 1;
      visibility: show;">
<img id='animation5' src ='http://i51.tinypic.com/sxheeo.gif'/>
</div>
<div
   style="
      top: 350;
      left: 700;
      position: absolute;
      z-index: 1;
      visibility: show;">
<img id='animation6' src ='http://i51.tinypic.com/sxheeo.gif'/>
</div>
<div
   style="
      top: 350;
      left: 33;
      position: absolute;
      z-index: 1;
      visibility: show;">
<img id='animation7' src ='http://i51.tinypic.com/sxheeo.gif'/>

</div>
<div
   style="
      top: 350;
      left: 352;
      position: absolute;
      z-index: 1;
      visibility: show;">
<img id='animation8' src ='http://i51.tinypic.com/sxheeo.gif'/>
</div>
</body>
<head>
<script type="text/javascript">
var urls;

function animate0(pos) {
    pos %= urls.length;
    var animation0 = document.getElementById('animation0');
    var counter = document.getElementById('counter');
    animation0.src = urls[pos];
    if (pos == 1) { // only make onclick when have a certain image
        animation0.onclick = function() {
            counter.innerHTML = parseInt(counter.innerHTML) + 1;
        }
    }
    else {
        animation0.onclick = function() {
            //do nothing
        }
    }
    setTimeout(function() {
        animate0(++pos);
    }, (Math.random()*500) + 1000);
}
function animate1(pos) {
    pos %= urls.length;
    var animation1 = document.getElementById('animation1');
    var counter = document.getElementById('counter');
    animation1.src = urls[pos];
    if (pos == 1) { // only make onclick when have a certain image
        animation1.onclick = function() {
            counter.innerHTML = parseInt(counter.innerHTML) + 1;
        }
    }
    else {
        animation1.onclick = function() {
            //do nothing
        }
    }
    setTimeout(function() {
        animate1(++pos);
    }, (Math.random()*500) + 1000);
}
function animate2(pos) {
    pos %= urls.length;
    var animation2 = document.getElementById('animation2');
    var counter = document.getElementById('counter');
    animation2.s开发者_运维知识库rc = urls[pos];
    if (pos == 1) { // only make onclick when have a certain image
        animation2.onclick = function() {
            counter.innerHTML = parseInt(counter.innerHTML) + 1;
        }
    }
    else {
        animation2.onclick = function() {
            //do nothing
        }
    }
    setTimeout(function() {
        animate2(++pos);
    }, (Math.random()*500) + 1000);
}
function animate3(pos) {
    pos %= urls.length;
    var animation3 = document.getElementById('animation3');
    var counter = document.getElementById('counter');
    animation3.src = urls[pos];
    if (pos == 1) { // only make onclick when have a certain image
        animation3.onclick = function() {
            counter.innerHTML = parseInt(counter.innerHTML) + 1;
        }
    }
    else {
        animation3.onclick = function() {
            //do nothing
        }
    }
    setTimeout(function() {
        animate3(++pos);
    }, (Math.random()*500) + 1000);
}
function animate4(pos) {
    pos %= urls.length;
    var animation4 = document.getElementById('animation4');
    var counter = document.getElementById('counter');
    animation4.src = urls[pos];
    if (pos == 1) { // only make onclick when have a certain image
        animation4.onclick = function() {
            counter.innerHTML = parseInt(counter.innerHTML) + 1;
        }
    }
    else {
        animation4.onclick = function() {
            //do nothing
        }
    }
    setTimeout(function() {
        animate4(++pos);
    }, (Math.random()*500) + 1000);
}
function animate5(pos) {
    pos %= urls.length;
    var animation5 = document.getElementById('animation5');
    var counter = document.getElementById('counter');
    animation5.src = urls[pos];
    if (pos == 1) { // only make onclick when have a certain image
        animation5.onclick = function() {
            counter.innerHTML = parseInt(counter.innerHTML) + 1;
        }
    }
    else {
        animation5.onclick = function() {
            //do nothing onclick
        }
    }
    setTimeout(function() {
        animate5(++pos);
    }, (Math.random()*500) + 1000);
}
function animate6(pos) {
    pos %= urls.length;
    var animation6 = document.getElementById('animation6');
    var counter = document.getElementById('counter');
    animation6.src = urls[pos];
    if (pos == 1) { // only make onclick when have a certain image
        animation6.onclick = function() {
            counter.innerHTML = parseInt(counter.innerHTML) + 1;
        }
    }
    else {
        animation6.onclick = function() {
            //do nothing
        }
    }
    setTimeout(function() {
        animate6(++pos);
    }, (Math.random()*500) + 1000);
}
function animate7(pos) {
    pos %= urls.length;
    var animation7 = document.getElementById('animation7');
    var counter = document.getElementById('counter');
    animation7.src = urls[pos];
    if (pos == 1) { // only make onclick when have a certain image
        animation7.onclick = function() {
            counter.innerHTML = parseInt(counter.innerHTML) + 1;
        }
    }
    else {
        animation7.onclick = function() {
            //do nothing
        }
    }
    setTimeout(function() {
        animate7(++pos);
    }, (Math.random()*500) + 1000);
}
function animate8(pos) {
    pos %= urls.length;
    var animation8 = document.getElementById('animation8');
    var counter = document.getElementById('counter');
    animation8.src = urls[pos];
    if (pos == 1) { // only make onclick when have a certain image
        animation8.onclick = function() {
            counter.innerHTML = parseInt(counter.innerHTML) + 1;
        }
    }
    else {
        animation8.onclick = function() {
            //do nothing
        }
    }
       setTimeout(function() {
        animate8(++pos);
    }, (Math.random()*500) + 1000);
}
window.onload = function() { //Frames go below, seperated by commas format= , "URL");
    urls = new Array("http://i51.tinypic.com/sxheeo.gif", "http://i56.tinypic.com/2i3tyw.gif");
    animate0(0);
    animate1(0);
    animate2(0);
    animate3(0);
    animate4(0);
    animate5(0);
    animate6(0);
    animate7(0);
    animate8(0);
}
</script>
</head>
</html>


Generate 9 random numbers between 0 and your start time variation. Decide the max distance you want between the animations and scale the random numbers to that time frame. Then, set 9 timers to those times from now so each timer starts an animation.

If you wanted the animations to start over 500 milliseconds, you'd do something like this:

var randomTimes = [9];

for (var i = 0; i < 9; i++) {
    randomTimes[i] = Math.floor(Math.random() * 501);
}

Now you have 9 random times spread out over 500 milliseconds and you can use those values with setTimeout to start each animation at a random time.


Copy/pasting a function this way is indeed sloppy!

You need to setup a flag:

var isMole  = false;//at first there is no mole
if(!isMole){    
   //there is no mole, you can show one here    
   isMole = true;//there is a mole now!
}else{
   //there is a mole, we wait.
}

Now set that flag to false when the mole times out or it struck by a hammer!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号