【问题标题】:Method createjs.Stage return an error with easeljs方法 createjs.Stage 使用 easeljs 返回错误
【发布时间】:2023-03-03 09:11:17
【问题描述】:

当我得到画布并发送创建阶段时,我得到这个字符串错误:

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

【问题讨论】:

    标签: javascript html5-canvas easeljs


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      canvas = document.getElementById("testCanvas");
      

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

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

      【讨论】:

        【解决方案3】:

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

        @这个库的开发者:按照 Douglas Crockford 的建议(参见link),循环应该首先检查它的属性:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-07
          • 2020-08-10
          • 2014-07-17
          • 2016-02-01
          • 2011-04-24
          相关资源
          最近更新 更多