2017-06-05 29 views
0

這看起來很簡單,但我對JavaScript有點新鮮和掙扎。該文檔舉這個例子:無法將Threejs EventDispatcher附加到自定義對象

// 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,它實現了此事件。在我的app.js文件中加載,這是我粘貼示例代碼的地方。此時它拋出EventDispatcher is not defined。如果在此之前單獨拉入EventDispatcher.js,則一切正常。

我想這是一個簡單的加載/執行順序的事情,我想知道如何解決這個問題,而不需要額外的EventDispatcher.js位。乾杯!

回答

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

+0

完美,乾杯! – tbriley

相關問題