设置存储在数组中的three.js对象的位置

时间:2018-02-01 15:29:51

标签: javascript three.js game-engine

每次调用我的animate函数时,我都试图在x轴+ 1上移动我的对象,但我一直遇到错误cannot read property x of undefined

所以每当我在场景中添加网格时,我都会将其添加到sceneObjects数组

像这样

addToGlScene(mesh, 'player')

使用此功能

    function addToGlScene(what, name, glow, storage){
        glScene.add(what)
        what.name = name
        sceneObjects.push(what)
        what.userData.glow = {glow: glow}
        what.userData.storage = {storage: storage}
        sceneObjects[name] = what
    }

所以我尝试在我的animate()函数

中使用此方法再次更改对象位置
    sceneObjects['player'].position

但我仍然得到同样的错误

即使我console.log(sceneObjects['player']) 它显示了所有这些

    THREE.MorphAnimMesh {uuid: "42A8C528-F4E4-4317-9516-63B7B717BEF8", name: "player", type: "Mesh", parent: vd, children: Array(0), …}
    castShadow:
      false
      children:[]
      currentKeyframe:40
      direction:1
      directionBackwards:false
      drawMode:0
      duration:1000
      endKeyframe:163
      frustumCulled:true
      geometry:L {uuid: "5C675298-D5DD-4910-B98A-84D71A036636", name: "", type: "Geometry", vertices: Array(300), colors: Array(0), …}
      lastKeyframe:39
      layers:Wd {mask: 1}
      length:164
      material:Aa {uuid: "7E4FB381-61C2-4C33-8E08-C1997F94F0A4", name: "", type: "MeshBasicMaterial", fog: true, lights: false, …}
      matrix:O {elements: Array(16)}
      matrixAutoUpdate:true
      matrixWorld:O {elements: Array(16)}
      matrixWorldNeedsUpdate:false
      mirroredLoop:false
      morphTargetDictionary:{stand000î´: 0, stand001î´: 1, stand002î´: 2, stand003î´: 3, stand004î´: 4, …}
      morphTargetInfluences:(164) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.29446199999998723, 0.7055380000000128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …]
      name:"player"
      parent:vd {uuid: "C328C91A-D445-4E48-9C74-A15A4FA318AB", name: "", type: "Scene", parent: null, children: Array(13), …}
      position:p {x: 0, y: 0, z: 0}
      quaternion:ha {_x: 0, _y: 0, _z: 0, _w: 1, onChangeCallback: ƒ}
      receiveShadow:false
      renderOrder:0
      rotation:gb {_x: 0, _y: 0, _z: 0, _order: "XYZ", onChangeCallback: ƒ}
      scale:p {x: 1, y: 1, z: 1}
      startKeyframe:0
      time:248.20450000000008
      type:"Mesh"
      up:p {x: 0, y: 1, z: 0}
      userData:{glow: {…}, storage: {…}}
      uuid:"42A8C528-F4E4-4317-9516-63B7B717BEF8"
      visible:true
      eulerOrder:(...)
      id:28
      modelViewMatrix:O {elements: Array(16)}
      normalMatrix:ma {elements: Array(9)}
      useQuaternion:(...)
      __proto__:ra

这是我的animate()函数

        function animate() {
            if(typeof mesh != "undefined") mesh.updateAnimation(clock.getDelta() * 100);
            requestAnimationFrame( animate );
            glRenderer.render(glScene, camera);
            cssRenderer.render(cssScene, camera);
            if ( controlsEnabled === true ) {
                var time = performance.now();
                var delta = ( time - prevTime ) / 1000;
                velocity.x -= velocity.x * 10.0 * delta;
                velocity.z -= velocity.z * 10.0 * delta;
                velocity.y -= 9.8 * 40.0 * delta; // 100.0 = mass
                direction.y = Number( flyDown) - Number(flyUp);
                direction.z = Number( moveForward ) - Number( moveBackward );
                direction.x = Number( moveLeft ) - Number( moveRight );
                direction.normalize(); // this ensures consistent movements in all directions
                if (flyUp || flyDown) velocity.y -= direction.y * player.speed * delta;
                if ( moveForward || moveBackward ) velocity.z -= direction.z * player.speed * delta;
                if ( moveLeft || moveRight ) velocity.x -= direction.x * player.speed * delta;
                controls.getObject().translateX( velocity.x * delta );
                controls.getObject().translateY( velocity.y * delta );
                controls.getObject().translateZ( velocity.z * delta );
                if ( controls.getObject().position.y < player.heigth) {
                    velocity.y = 0;
                    controls.getObject().position.y = player.heigth;
                    canJump = true;
                }
                prevTime = time;
            }
            glRenderer.render( glScene, camera );
        }

我做错了什么?

2 个答案:

答案 0 :(得分:1)

这应该有效:

sceneObjects['player'].position.x = newXValue;

position属性为Vector3,您可以从中访问x属性。

如果动画函数中未定义sceneObjects['player'].position,则其他错误。

答案 1 :(得分:0)

我自己明白了:)我不断收到undefined错误的原因是因为我在动画函数中调用了console.log(sceneObjects['player'].position),一旦页面加载就会被激活模型尚未加载,因此我使用tree.js Loading Manager仅在所有对象加载后调用animate函数并且它完美地运行