我正在使用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找到
发布于 2015-06-06 12:24:50
尝尝这个
this.stage.removeChild(event.target.parent)这是因为event.target是形状的,其父是平铺的,基本上您想要移除瓷砖,所以这是可行的。
对于您将来的参考,我从本文档http://www.createjs.com/docs/easeljs/classes/Stage.html中找到了它。
https://stackoverflow.com/questions/30682015
复制相似问题