Uncaught TypeError:无法读取未定义的属性'propeller'

问题描述 投票:1回答:1

我正在尝试完成本教程

https://tympanus.net/codrops/2016/04/26/the-aviator-animating-basic-3d-scene-threejs/

我收到此错误:

未捕获的TypeError:无法读取未定义的属性'propeller';

它看起来像这样:

                 function loop(){
                     airplane.propeller.rotation.x += 0.3;
                     sea.mesh.rotation.z += .005;
                     sky.mesh.rotation.z += .01;

                     updatePlane();

                    renderer.render(scene, camera);

                    requestAnimationFrame(loop);
                   }

我尝试对其进行修改:

                   function loop(){
                      var airplane;

                      airplane.propeller.rotation.x += 0.3;
                      sea.mesh.rotation.z += .005;
                      sky.mesh.rotation.z += .01;

                      updatePlane();

                      renderer.render(scene, camera);

                      requestAnimationFrame(loop);
                     }

                 function loop(){
                     var airplane = new airplane();
                     airplane.propeller.rotation.x += 0.3;
                     sea.mesh.rotation.z += .005;
                     sky.mesh.rotation.z += .01;

                     updatePlane();

                     renderer.render(scene, camera); //START THE ANIMATION,

                     requestAnimationFrame(loop);
                    }

但它给出了相同的错误;

我想念什么?

javascript three.js 3d
1个回答
2
投票

您必须确保在airplane函数可见的范围内声明loop()。恐怕您的修改没有任何意义。第一个修改只是声明一个变量,这意味着它在下一行中仍为undefined。第二个步骤在每个动画步骤中都会创建飞机,这当然也不对(您只想创建一次飞机3D对象,然后再使用它)。

本教程将像这样创建飞机:

var airplane;

function createPlane(){ 
    airplane = new AirPlane();
    airplane.mesh.scale.set(.25,.25,.25);
    airplane.mesh.position.y = 100;
    scene.add(airplane.mesh);
}

只需确保避免重新声明airplane

© www.soinside.com 2019 - 2024. All rights reserved.