[英]how to run simultaneous animations at different speeds javascript
This code does the job now. 此代码现在可以完成工作。 The animations are all within the function calls . 动画都在函数调用中。
function animate() { 函数animate(){
requestAnimationFrame(animate);
for (var i = 0; i < objects.length; i++) {
var object = objects[i];
if (--object.countdown <= 0) {
object.countdown = object.delay;
if(i==0){
advanceTheFleet();
if(dropLine){
object.delay-=20;
if(object.delay<=10){
object.delay=10;
}
}
}
else{
propelMissiles();
}
}
}
} animate(); } animate();
Demo: http://jsfiddle.net/m1erickson/4vV5X/ 演示: http : //jsfiddle.net/m1erickson/4vV5X/
Start with one requestAnimationFrame (because it's efficient at animation loops). 从一个requestAnimationFrame开始(因为它在动画循环中非常有效)。
I'm assuming you have created objects for your ship and missle. 我假设您已经为飞船和导弹创建了对象。
Add a delay
and countdown
element to each object 向每个对象添加一个delay
和countdown
元素
objects.push({id:"ship",x:20,y:20,vx:1,vy:0,delay:20,countdown:20,color:"green"});
objects.push({id:"missle",x:220,y:20,vx:-1,vy:0,delay:3,countdown:3,color:"red"});
Then in requestAnimationFrame you can reduce the countdown for each object. 然后,在requestAnimationFrame中,您可以减少每个对象的倒计时。
If the countdown reaches zero you draw that object. 如果倒数达到零,则绘制该对象。
for(var i=0;i<objects.length;i++){
var object=objects[i];
// if this object's countdown has expired, move it
if(--object.countdown<=0){
// reset the countdown
object.countdown=object.delay;
// move the object
object.x+=object.vx;
object.y+=object.vy;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.