removeChild() 无法使用 easeljs

removeChild() not working using easeljs

我正在使用 easeljs 创建一个舞台,然后我将瓷砖放在舞台上的随机位置。当用户单击一个图块时,该图块必须从舞台上移除。 我面临两个问题。首先是鼠标事件不起作用。代码 "tile.onPress = (tileOnPress).bind(this);" 所以我改用了 addEventListener 方法。现在,尽管在我在控制台上获取输出时正在调用该函数,但该图块并未从舞台上移除。 这是代码:

c99.Game = (function(){
function Count99Game() {
    console.log("Count 99 game starts");

    this.canvas = document.getElementById('game-canvas');

    this.stage = new createjs.Stage(this.canvas);

    var totalTiles = 10;

    var tileOnPress = function(event) {
        console.log("Pressed");
        this.stage.removeChild(event.target);
        this.stage.update();
    };  

    for (var i = totalTiles; i > 0; i--) {
        var tile = new c99.Tile(i);
        this.stage.addChild(tile);
        tile.x = Math.random()*(this.canvas.width - tile.width);
        tile.y = Math.random()*(this.canvas.height - tile.height);

        //tile.onPress = (tileOnPress).bind(this);
        tile.addEventListener("click", tileOnPress.bind(this));
    }

    this.stage.update();
}   

return Count99Game;
})();

如果有人能告诉我为什么 "tile.onPress = (tileOnPress).bind(this);" 不起作用,以及需要在 tileOnPress 函数中进行哪些更改,以便可以从舞台上移除按下的图块,我将不胜感激。

完整代码见https://github.com/ZANTGames/count99/blob/master/js/count99-game.js

试试这个

this.stage.removeChild(event.target.parent)

这是因为 event.target 是形状,它的父级是瓷砖,基本上你想删除瓷砖,所以这是有效的

为了您将来的参考,我从这个文档中找到了它 http://www.createjs.com/docs/easeljs/classes/Stage.html