简体   繁体   English

Object 在 Function 调用后变为数字 - Javascript

[英]Object Becomes Number After Function Call - Javascript

I am using Three.js to create an animation, but my animate() function is not working properly.我正在使用 Three.js 创建 animation,但我的animate() function 无法正常工作。 Simplified code below:简化代码如下:

let obj;
let camera = new THREE.PerspectiveCamera(fov, asp, near, far);
let scene = new THREE.Scene();
const loader = new THREE.GLTFLoader();
async function init() {
    obj = await loader.loadAsync("./public/modelo/scene.gltf");
    scene.add(obj.scene);
    animate(obj.scene.children[0]);
    renderer.render(scene, camera);
    function animate(objeto){
        console.log(objeto);
        requestAnimationFrame(animate);
        objeto.rotation.z += 0.005;
        renderer.render(scene, camera);
    }
}
init();

The function works for the first call, but after that I can see things going wrong via my console.log() statement. function 适用于第一次通话,但之后我可以通过我的console.log()语句看到出现问题。 On the first run of animate() , the line prints out the object, as expected.animate()的第一次运行中,该行按预期打印出 object。 But, on all ensuing calls, the statement just returns a floating point number that increases greatly with each successive call.但是,在所有随后的调用中,该语句只返回一个浮点数,每次连续调用都会大大增加。 I checked and made sure that the object was intact at the end of the first function call, so I'm not sure what's going on here.我检查并确保 object 在第一次 function 调用结束时完好无损,所以我不确定这里发生了什么。 Any suggestions?有什么建议么?

The problem is in this one line in your code:问题出在代码中的这一行:

requestAnimationFrame(animate);

From the MDN documentation for requestAnimationFrame , the callback you pass it - here animate , is:requestAnimationFrameMDN 文档中,您传递给它的回调 - 这里是animate ,是:

The function to call when it's time to update your animation for the next repaint. function 在需要更新 animation 以进行下一次重绘时调用。 The callback function is passed one single argument, a DOMHighResTimeStamp similar to the one returned by performance.now(), indicating the point in time when requestAnimationFrame() starts to execute callback functions.回调 function 传递了一个参数,一个类似于 performance.now() 返回的 DOMHighResTimeStamp,指示 requestAnimationFrame() 开始执行回调函数的时间点。

In other words, your animate is getting called with a timestamp - which in some ways I imagine behaves much like a number (I'm not personally familiar with what a DOMHighResTimeStamp is), and explains what you're seeing.换句话说,你的animate被一个时间戳调用 - 在某些方面我想象它的行为很像一个数字(我个人并不熟悉DOMHighResTimeStamp是什么),并解释了你所看到的。 It certainly won't be getting called with your "objeto" as argument.它肯定不会被你的“objeto”作为参数调用。

To ensure that, just pass in as a callback a function will calls animate with the correct argument.为确保这一点,只需将 function 作为回调传入,将使用正确的参数调用animate Replace the call above with将上面的调用替换为

requestAnimationFrame(() => { animate(objeto); });

EDIT: or as @Bergi suggests, we can avoid needing an argument at all by simply defining objeto outside animate , in the init function.编辑:或者正如@Bergi 建议的那样,我们可以通过在init function 中简单地在animate之外定义objeto来完全避免需要参数。 Since you always call animate with this same argument there's no need to specifically pass it, so your code can just be:由于您总是使用相同的参数调用animate ,因此无需专门传递它,因此您的代码可以是:

async function init() {
    obj = await loader.loadAsync("./public/modelo/scene.gltf");
    const objeto = obj.scene.children[0];
    scene.add(obj.scene);
    animate();
    renderer.render(scene, camera);
    function animate(){
        console.log(objeto);
        requestAnimationFrame(animate);
        objeto.rotation.z += 0.005;
        renderer.render(scene, camera);
    }
}

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

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