无法将Threejs EventDispatcher附加到自定义对象

时间:2017-06-05 05:43:49

标签: javascript three.js

这看起来非常简单,但我对JavaScript有点陌生并且苦苦挣扎。文档给出了这个例子:

// Adding events to a custom object

var Car = function () {

    this.start = function () {

        this.dispatchEvent( { type: 'start', message: 'vroom vroom!' } );
    };
};

// Mixin the EventDispatcher.prototype with the custom object prototype
Object.assign( Car.prototype, EventDispatcher.prototype );

// Using events with the custom object
var car = new Car();

car.addEventListener( 'start', function ( event ) {

    alert( event.message );
});

car.start();

在我的主页面的标题中,我包含three.js,它实现了EventDispatcher。在我的app.js中加载,这是我坚持示例代码的地方。此时它抛出EventDispatcher is not defined。如果我在此之前单独提取EventDispatcher.js,则一切正常。

我想这是一个简单的加载/执行命令,我想知道如何在不需要额外的EventDispatcher.js位的情况下解决这个问题。干杯!

1 个答案:

答案 0 :(得分:1)

这应该有效:

var Car = function () {

    this.start = function () {

        this.dispatchEvent( { type: 'start', message: 'vroom vroom!' } );
    };
};

Object.assign( Car.prototype, THREE.EventDispatcher.prototype );

var car = new Car();

car.addEventListener( 'start', function ( event ) {

    alert( event.message );

});

car.start();

three.js r.85

相关问题