1

当我获得画布并发送创建舞台时,我收到此字符串错误:

Uncaught TypeError: Cannot call method 'addEventListener' of undefined.

我的初始化函数:

function init() {
    canvas = document.getElementById("testCanvas");

    stage = new createjs.Stage(canvas);
    screen_width = canvas.width;
    screen_height = canvas.height;

    contentManager = new ContentManager();
    contentManager.SetDownloadCompleted(startGame);
    contentManager.StartDownload();
}

给出错误的代码块是:

p.enableDOMEvents = function(enable) {
    if (enable == null) { enable = true; }
    var n, o, ls = this._eventListeners;
    if (!enable && ls) {
        for (n in ls) {
            o = ls[n];
            o.t.removeEventListener(n, o.f);
        }
        this._eventListeners = null;
    } else if (enable && !ls) {
        var t = window.addEventListener ? window : document;
        var _this = this;
        ls = this._eventListeners = {};
        ls["mouseup"] = {t:t, f:function(e) { _this._handleMouseUp(e)} };
        ls["mousemove"] = {t:t, f:function(e) { _this._handleMouseMove(e)} };
        ls["dblclick"] = {t:t, f:function(e) { _this._handleDoubleClick(e)} };
        t = this.canvas;
        if (t) { ls["mousedown"] = {t:t, f:function(e) { _this._handleMouseDown(e)} }; }

        for (n in ls) {
            o = ls[n];
            o.t.addEventListener(n, o.f);
        }
    }

特别是这个块:

    for (n in ls) {
        o = ls[n];
        o.t.addEventListener(n, o.f);
    }

当 'n' 的值为 时BitmapAnimation_initialize,给出错误。我在Stage.js中找到了这段代码,该文件附带了easeljs v0.6.0的rar

4

3 回答 3

0

如果您注释掉 contentManager 代码,您是否看到此错误?似乎很奇怪。

于 2013-04-26T02:56:32.380 回答
0
canvas = document.getElementById("testCanvas");

你真的有一个 id 为“testCanvas”的 DOM 元素吗?

我在构建我的第一个 EaselJS 应用程序时遇到了这个错误,因为我在 id 中有错字...

于 2013-06-19T14:37:44.947 回答
0

您可能在object(我也是)的原型中添加了一些东西。javascript for-each 循环不区分“真实”和原型属性。

@developers of this library:根据 Douglas Crockford 的建议(见链接),循环应该首先检查它的属性:

for (n in ls) {
  if (ls.hasOwnProperty(n)) {
    o = ls[n];
    o.t.addEventListener(n, o.f);
  } 
}
于 2014-08-29T13:01:01.393 回答