简体   繁体   English

同时调用两个对象的递归函数

[英]Simultaneous call of recursive function for two objects

I created a recursive function, which animate an object with some mathematical laws: 我创建了一个递归函数,该函数使用一些数学定律为对象设置动画:

var pi = Math.PI; 
var divisions = 36;
var inc = pi/divisions;
var t=pi/2;
var i = 0;
var loopNumber = 0;
var time = 50;


function updateTime(obj, alp)       {

    obj = obj;
    alpha = alp;

    x1 = //some mathematical calculations, x1 = f (t, alpha);
    y1 = //some mathematical calculations, y1 = f (t, alpha);

    if (loopNumber ==5) {
        obj.stop();
        return
    }

    if (i == divisions*2) {
        t=pi/2;
        i=0;
        loopNumber++;
    }

    t+= inc;    
    i++

    obj.animate({
        top: y1,
        left: x1,
    }, time, 'linear', updateTime(obj, alpha))


}   

It works perfect for one object: 它非常适合一个对象:

$(window).load( function() {
        updateTime($('#runner1'), pi/4);
    }
)

But when I tried to call this function for two objects - I faced with a trouble - it always works for one object. 但是,当我尝试为两个对象调用此函数时-我遇到了麻烦-它始终对一个对象起作用。 Another object don't animate: 另一个对象不设置动画:

$(window).load( function() {
        updateTime($('#runner1'), pi/4);
        updateTime($('#runner2'), -pi/4);
    }
)

Can you explain plese - why? 您能解释一下优美吗-为什么? How to make it work simultaneously for several objects? 如何使它同时对几个对象起作用?

HTML is: HTML是:

<div id="runner1" style="height: 20px; width: 20px; border-radius: 10px; box-shadow: 0 0 13px gold; background-color: cyan; z-index: 1; position: absolute; margin-left: -10px;  margin-top: -10px;"> </div>
<div id="runner2" style="height: 20px; width: 20px; border-radius: 10px; box-shadow: 0 0 13px red; background-color: violet; z-index: 1; position: absolute;  margin-left: -10px;  margin-top: -10px;"> </div>

You need to wrap the recursive call inside an anonymous function, otherwise it is executed immediately when animate states. 您需要将递归调用包装在匿名函数中,否则在设置动画状态时立即执行。 It is purely because the animations are queued for the same element that it even works for one item: 纯粹是因为动画排队的元素与它甚至可用于一项的元素相同:

    obj.animate({
        top: y1,
        left: x1,
    }, time, 'linear', function(){updateTime(obj, alpha);})

eg 例如

function updateTime(obj, alp)       {

    obj = obj;
    alpha = alp;

    x1 = //some mathematical calculations, x1 = f (t, alpha);
    y1 = //some mathematical calculations, y1 = f (t, alpha);

    if (loopNumber ==5) {
        obj.stop();
        return
    }

    if (i == divisions*2) {
        t=pi/2;
        i=0;
        loopNumber++;
    }

    t+= inc;    
    i++

    obj.animate({
        top: y1,
        left: x1,
    }, time, 'linear', function(){updateTime(obj, alpha);})


}   

Note: Accessing a global variable for the loop will cause you problems. 注意:为循环访问全局变量将导致您遇到问题。 You should pass a countdown as well and pass countdown-1 to the recursive calls. 您还应该传递倒计时,并将countdown-1传递给递归调用。 I am not sure what some of your other variables are doing, so you may be able to remove more code now: 我不确定您的某些其他变量在做什么,因此您现在可以删除更多代码:

function updateTime(obj, alp, countdown)       {

    obj = obj;
    alpha = alp;

    x1 = //some mathematical calculations, x1 = f (t, alpha);
    y1 = //some mathematical calculations, y1 = f (t, alpha);

    if (!countdown) {
        obj.stop();
        return
    }

    if (i == divisions*2) {
        t=pi/2;
        i=0;
    }

    t+= inc;    
    i++

    obj.animate({
        top: y1,
        left: x1,
    }, time, 'linear', function(){updateTime(obj, alpha, countdown-1);})


}   

and use like this (to run each with 5 animations): 并像这样使用(以5个动画运行每个动画):

$(window).load( function() {
        updateTime($('#runner1'), pi/4, 5);
        updateTime($('#runner2'), -pi/4, 5);
    }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM