Javascript面向对象:"未捕获的TypeError:无法读取属性' animate'"

时间:2017-08-10 12:42:42

标签: javascript oop three.js

我正在开始面向对象的JavaScript编程。 我使用WebGL在画布上绘制3d场景,使用three.js lib来简化WebGl。

我的对象Animator遇到了问题:

class Animator
{
    constructor(sceneObjects)
    {
        alert("initiating Animator");
        var _sceneObjects = sceneObjects;
        var _lon = 0, _lat = 0, _phi = 0, _theta = 0;

        this.animate = function()
        {
            requestAnimationFrame(this.animate);
            this.update();
        }

        this.update = function()
        {
            _lat = Math.max(-85, Math.min(85, _lat));
            _phi = THREE.Math.degToRad(90 - _lat);
            _theta = THREE.Math.degToRad(_lon);
            _sceneObjects.getCamera().target.x = 500 * Math.sin(_phi) * Math.cos(_theta);
            _sceneObjects.getCamera().target.y = 500 * Math.cos(_phi);
            _sceneObjects.getCamera().target.z = 500 * Math.sin(_phi) * Math.sin(_theta);
            _sceneObjects.getCamera().lookAt(_sceneObjects.getCamera().target);

            // distortion
            //camera.position.copy( camera.target ).negate();

            _sceneObjects.getRenderer().render(_sceneObjects.getScene(), _sceneObjects.getCamera());
        }

        //setters
        this.setLon = function(val) {
            _lon = val;
        }
        this.setLat = function(val) {
            _lat = val;
        }
        this.setPhi = function(val) {
            _phi = val;
        }
        this.setTheta = function(val) {
            _theta = val;
        }
        //getters
        this.getSceneObjects = function() {
            return _sceneObjects;
        }
        this.getLon = function() {
            return _lon;
        }
        this.getLat = function() {
            return _lat;
        }
        this.getPhi = function() {
            return _phi;
        }
        this.getTheta = function() {
            return _theta;
        }
    }
}

Animator.animate()函数应该在画布中绘制图像,它由Three.js库给出。 我已经让它在一个对象之外工作了。只有,当我现在调用Animator.update()时,此错误会弹出:

Animator.js:12 Uncaught TypeError: Cannot read property 'animate' of undefined
    at Animator.animate 

为什么我无法阅读它?有任何想法吗 ? 谢谢!

0 个答案:

没有答案