如何通过requestAnimationFrame传递参数? [英] How can I pass argument with requestAnimationFrame?

查看:54
本文介绍了如何通过requestAnimationFrame传递参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在主程序中,我随机选择一个要设置动画的对象,因此我以该对象作为参数调用该函数.第一个循环没问题, x 设置正确,但是在下一回合中,它变成了 undefined .

In the main program I randomly choose an object which I'd like to animate, so I call the function with the object as the argument. The first loop is okay, x is finely set, but in the next turn it becomes undefined.

类似这样的东西:

var anim = {
        mainFunc: function(x) {
            anim.update(x);
            anim.redraw(x);
            window.requestAnimationFrame(anim.mainFunc);
        },

        update: function(x) {

        },

        redraw: function(x) {

        }
};

var n=Math.floor(Math.random() * (ArrayOfAnimObject.length));
anim.mainFunc(ArrayOfAnimObject[n]);

推荐答案

您需要创建引用或将函数调用包装在另一个函数中,如下所示:

You either need to create a reference or wrap the function call in another function like so:

mainFunc: function(x) {
    anim.update(x);
    anim.redraw(x);
    window.requestAnimationFrame(function() {
        anim.mainFunc(x);
    });
}

这篇关于如何通过requestAnimationFrame传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆