2016-11-16 25 views
0

以下代碼是正確的?EaselJS中的EventDispatcher

function MyClass() 
{ 
    createjs.EventDispatcher.initialize(this); 
    var _that = this; 
    ... 
    function _onCompletedFunc() 
    { 
     var user_event = new createjs.Event("completed"); 
     user_event.label = "my label"; 
     _that.dispatchEvent(user_event); 
    } 
} 

我正確理解EaselJS中的EventDispatcher嗎? 謝謝。

回答

0

這看起來沒問題,雖然方法有點奇怪。它不起作用嗎?下面是一個快速示例:http://jsfiddle.net/lannymcnie/b6b6ddqo/

此方法的主要問題是「_onCompletedFunc」不是類的成員,它只存在於MyClass構造函數的作用域中。這意味着只有該方法可以調用它。一個更好的方法是把它放在原型上。

MyClass.prototype._onCompletedFunc = function() { 
    var user_event = new createjs.Event("completed"); 
    user_event.label = "my label"; 
    this.dispatchEvent(user_event); 
} 

http://jsfiddle.net/lannymcnie/b6b6ddqo/2/

希望幫助!

+0

謝謝蘭尼) –