2014-03-02 53 views
0

我嘗試了創建一個小型俄羅斯方塊遊戲以進入javascript。我遇到了一些與easeljs有關的問題,但是我能夠爲他們中的大多數人找到解決方案。但有一個問題我無法弄清楚。我是這樣做的:容器的Easeljs子項在拆除後仍然顯示在畫布上

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); 

正如你所看到的tetrisBlocks是容器中有幾個形狀。在某些時候,當我檢測到一條完整的線時,我會這樣做:

this.removeChild(this.childs [index]);

如果這是一個tetrisBlock,並且this.childs [index]是tetrisBlock內部的形狀之一。問題是:在我完成之後,成功刪除了該形狀,但即使在stage.update()命令之後它仍然顯示在畫布上。

如果我將整個tetrisBlock從舞臺上移除,但那不是我想要的。我想刪除一個俄羅斯方塊的幾個孩子。

回答

0

得到它改變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; 
}()); 
相關問題