分三.js前进

Moving forward in three.js

本文关键字:前进 js 分三      更新时间:2023-09-26
    var leftKey = 72;
    var rightKey = 74;

    document.onkeydown = checkKey;
    function checkKey(e){
        e = e || window.event;
    if(e.keyCode === leftKey){
           car.position.z = car.position.z -10;
       }
    if(e.keyCode === rightKey){
           car.position.z = car.position.z +10;
       }
    } 
    car.position.x ++;

您好,我正在尝试在游戏的每次更新中移动汽车。但是代码"car.position.x +++;"抛出错误"类型错误:无法读取未定义的属性'position'"。我该如何解决这个问题?谢谢!

myColladaLoader = new THREE.ColladaLoader();
myColladaLoader.options.convertUpAxis = true;
myColladaLoader.load( 'car.DAE', function ( collada ) {
        // Her the dae in a global variable.
        car = collada.scene;
        // Position your model in the scene (world space).
        car.position.x = -1850;
        car.position.y = 0;
        car.position.z = 0;
        // Scale your model to the correct size.
        car.scale.x = car.scale.y = car.scale.z = 0.2;
        car.updateMatrix();
        // Add the model to the scene.
        scene.add( car );
    } );

在上面添加汽车!

这里的问题显然不是范围错误,而是 javascript 渲染函数在实现 car 之前就更新了。解决这个问题的方法是推迟前进的汽车,直到到达那里。

请看三个.js文档