EaselJS中的EventDispatcher

时间:2016-11-16 07:04:41

标签: easeljs

以下代码是否正确?

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? 谢谢。

1 个答案:

答案 0 :(得分: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/

希望有所帮助!