Javascript 单击easeljs中形状的处理程序
我是一个初学者。我正在学习一个过时的教程。我想把编号的方块放在舞台上,然后当我点击它们时,让它们消失 原始代码有一个onPress事件,我已经将其修改为click侦听器。但是,我在单击处理程序中收到一条关于“this”对象范围的错误消息Javascript 单击easeljs中形状的处理程序,javascript,jquery,easeljs,Javascript,Jquery,Easeljs,我是一个初学者。我正在学习一个过时的教程。我想把编号的方块放在舞台上,然后当我点击它们时,让它们消失 原始代码有一个onPress事件,我已经将其修改为click侦听器。但是,我在单击处理程序中收到一条关于“this”对象范围的错误消息 TypeError: this.stage is undefined 我做错了什么 c99.Game = (function() { function Count99Game(){ this.canvas = document.getE
TypeError: this.stage is undefined
我做错了什么
c99.Game = (function() {
function Count99Game(){
this.canvas = document.getElementById('game-canvas');
this.stage = new createjs.Stage(this.canvas);
var totalTiles = 10;
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.addEventListener("click", function(event){
this.stage.removeChild(event.target);
this.stage.update();
}).bind(this);
}
this.stage.update();
}
return Count99Game;
})();
在EaselJS的
点击
处理程序中,这是窗口
对象。您需要将bind
调用移动到侦听器函数本身:
tile.addEventListener("click", function(event){
this.stage.removeChild(event.target);
this.stage.update();
}.bind(this));
您还可以使用以下方法手动设置处理程序的此:
在EaselJS的点击
处理程序中,这是窗口
对象。您需要将bind
调用移动到侦听器函数本身:
tile.addEventListener("click", function(event){
this.stage.removeChild(event.target);
this.stage.update();
}.bind(this));
您还可以使用以下方法手动设置处理程序的此:
因为我不熟悉这个框架,所以我更喜欢使用这个答案中提供的较短版本(如果可以的话)+1由于我不熟悉此框架,我更愿意使用本答案中提供的较短版本(如果可行)+1.