Javascript 移除后,容器的Easeljs子级仍显示在画布上
我试着创建一个小俄罗斯方块游戏来进入javascript。我在使用easeljs时遇到了一些问题,但我能够为大多数问题找到解决方案。但有一个问题我就是搞不懂。我是这样做的:Javascript 移除后,容器的Easeljs子级仍显示在画布上,javascript,easeljs,createjs,Javascript,Easeljs,Createjs,我试着创建一个小俄罗斯方块游戏来进入javascript。我在使用easeljs时遇到了一些问题,但我能够为大多数问题找到解决方案。但有一个问题我就是搞不懂。我是这样做的: tetrisI = function(x, y, size) { this.childs = new Array(); var rect1 = new createjs.Shape(); var rect2 = new createjs.Shape(); var rect3 = new cr
tetrisI = function(x, y, size) {
this.childs = new Array();
var rect1 = new createjs.Shape();
var rect2 = new createjs.Shape();
var rect3 = new createjs.Shape();
var rect4 = new createjs.Shape();
rect1.graphics.beginFill('#FF0000').drawRect(0, 0, size, size);
rect2.graphics.beginFill('#00FF00').drawRect(0, size, size, size);
rect3.graphics.beginFill('#0000FF').drawRect(0, size * 2, size, size);
rect4.graphics.beginFill('#FF0000').drawRect(0, size * 3, size, size);
this.addChild(rect1);
this.addChild(rect2);
this.addChild(rect3);
this.addChild(rect4);
this.childs.push(rect1);
this.childs.push(rect2);
this.childs.push(rect3);
this.childs.push(rect4);
}
tetrisI.prototype = new createjs.Container();
tetrisI.Container_initialize = tetrisI.initialize;
var stage = new createjs.Stage("mycanvas");
var tetrisBlock = new tetrisI(120, 0, gameState.blockSize);
stage.addChild(tetrisBlock);
如您所见,TetrisBlock是包含多个形状的容器。在某个时刻,当我检测到一条完整的线时,我会这样做:
tetrisI = function(x, y, size) {
this.childs = new Array();
var rect1 = new createjs.Shape();
var rect2 = new createjs.Shape();
var rect3 = new createjs.Shape();
var rect4 = new createjs.Shape();
rect1.graphics.beginFill('#FF0000').drawRect(0, 0, size, size);
rect2.graphics.beginFill('#00FF00').drawRect(0, size, size, size);
rect3.graphics.beginFill('#0000FF').drawRect(0, size * 2, size, size);
rect4.graphics.beginFill('#FF0000').drawRect(0, size * 3, size, size);
this.addChild(rect1);
this.addChild(rect2);
this.addChild(rect3);
this.addChild(rect4);
this.childs.push(rect1);
this.childs.push(rect2);
this.childs.push(rect3);
this.childs.push(rect4);
}
tetrisI.prototype = new createjs.Container();
tetrisI.Container_initialize = tetrisI.initialize;
var stage = new createjs.Stage("mycanvas");
var tetrisBlock = new tetrisI(120, 0, gameState.blockSize);
stage.addChild(tetrisBlock);
this.removeChild(this.childs[index])
其中,这是一个tetrisBlock,而这个.childs[index]是tetrisBlock内部的一个形状。问题是:在我这样做之后,形状被成功删除,但即使在执行stage.update()命令之后,它仍然显示在画布上
如果我把整个tetrisBlock从舞台上移除,它会工作,但那不是我想要的。我只想删除tetrisBlock的几个子项。将tetrisI函数更改为以下内容后,使其正常工作:
(function() {
var tetrisI = function(x, y, size) {
this.initialize(x, y, size);
}
var p = tetrisI.prototype = new createjs.Container();
tetrisI.prototype.Container_initialize = p.initialize;
tetrisI.prototype.initialize = function(x, y, size) {
this.Container_initialize();
// add custom setup logic here.
this.childs = new Array();
var rect1 = new createjs.Shape();
var rect2 = new createjs.Shape();
var rect3 = new createjs.Shape();
var rect4 = new createjs.Shape();
rect1.graphics.beginFill('#FF0000').drawRect(0, 0, size, size);
rect2.graphics.beginFill('#00FF00').drawRect(0, size, size, size);
rect3.graphics.beginFill('#0000FF').drawRect(0, size * 2, size, size);
rect4.graphics.beginFill('#FF0000').drawRect(0, size * 3, size, size);
this.addChild(rect1);
this.addChild(rect2);
this.addChild(rect3);
this.addChild(rect4);
this.childs.push(rect1);
this.childs.push(rect2);
this.childs.push(rect3);
this.childs.push(rect4);
this.regX = size / 2;
this.regY = size * 2;
this.size = size;
this.height = size * 4;
this.width = size;
this.x = x + this.regX;
this.y = y + this.regY;
tetrisI.prototype.pos = new Array(0, 0, 0, 1, 0, 2, 0, 3);
}
window.tetrisI = tetrisI;
}());