Uncaught TypeError: child.dispatchEvent is not a function EaselJS

Uncaught TypeError: child.dispatchEvent is not a function EaselJS

我正在尝试使用 EaselJS. I'm getting Uncaught TypeError: child.dispatchEvent is not a function EaselJS error when trying to add an object class to the stage it seems. I am using jQuery, but I've followed this SO post 创建纸牌游戏以消除 jQuery 冲突:

    <script src="lib/jQuery.js"></script>
    <script>jQuery.noConflict();</script>

我也在使用 RaphaelJS,但正在逐步淘汰它。我以为这个库也可能有冲突,所以我干脆完全删除了 jQueryraphaelJS 夹杂物,仍然有同样的错误。

代码片段:

this.tableCanvas = new createjs.Stage('Table');

function createDeck () {

        for (var i = 0; i < suits.length; i++) {
                for (var j = 0; j < values.length; j++) {
                    var card_data = { ... };
                    var card = new Card(this.tableCanvas, card_data);
                    ...
                }
         }
         this.tableCanvas.update();

Card class: 我正在控制台记录 canvas 以确保我没有丢失上下文,它实际上记录了正确的 Stage 对象.

var Card = function(canvas, args) {

    var graphics = new createjs.Graphics();
    graphics.beginStroke("red").beginFill("blue").drawRect(20, 20, 100, 50);
    this.shape = new createjs.Shape(graphics);
    this.set = new createjs.Container();
    this.set.addChild(this.shape);
    this.set.x = this.x;
    this.set.y = this.y;
    console.log('canvas', canvas);  //outputs createJS Stage object
    canvas.addChild(this); //Error seems to be because of this

我也试过在创建循环中添加卡片:

for ...
     for ...
         var card = new Card(this.tableCanvas, card_data);
         this.tableCanvas.addChild(card); 
     }
}

同样的错误。

HTML 片段:

<body>
    <div id="GameBoard">
        <canvas id="Table" width="1000" height="1000"></canvas>
    </div>
</body>

错误:createjs-2015.11.26.combined.js:7322

p.addChild = function(child) {
    if (child == null) { return child; }
    var l = arguments.length;
    if (l > 1) {
        for (var i=0; i<l; i++) { this.addChild(arguments[i]); }
        return arguments[l-1];
    }
    if (child.parent) { child.parent.removeChild(child); }
    child.parent = this;
    this.children.push(child);
    child.dispatchEvent("added"); //ERROR HERE
    return child;
};

我认为这是因为您正在向舞台添加 Card 实例。舞台子项需要是 DisplayObjects。您应该将 this.set 添加到 canvas 中。

我也推荐你的第二种方法。

this.tableCanvas.addChild(card.set);