我将源代码复制并粘贴到我的代码中,然后将tetrominoes数组扩展为包含5个以上的形状。它们被正确绘制,但DrawNext函数存在容纳它们的问题。编写函数的最佳方法是什么,因此它将使用扩展数组?
我的代码:
private function DrawNext():void {
if (getChildByName("next")!=null) {
removeChild(getChildByName("next"));
}
var next_t:Sprite=new Sprite();
next_t.x=352;
next_t.name="next";
addChild(next_t);
next_t.graphics.lineStyle(0,0xFFFFFF);
//The problem is here! Everything in the Output windows points to either it or this method
//i and j check to see if a piece in the grid is empty (0) or full (1).
for (var i:int=0; i<tetrominoes[nextTetromino][0].length; i++) {
for (var j:int=0; j<tetrominoes[nextTetromino][0][i].length; j++) {
//When it encounters a 1, it draws a new sprite of a particular color
//The color is determined by the piece that is being drawn.
if (tetrominoes[nextTetromino][0][i][j]==1) {
next_t.graphics.beginFill(colors[nextTetromino]);
next_t.graphics.drawRect(tileSize*j,tileSize*i,tileSize,tileSize);
next_t.graphics.endFill();
}
}
}
}
private function BuildTetrominoes():void {
// I
tetrominoes[0]=[[[0,0,0,0],[1,1,1,1],[0,0,0,0],[0,0,0,0]],
[[0,1,0,0],[0,1,0,0],[0,1,0,0],[0,1,0,0]]];
colors[0]=0x00FFFF; //Cyan
// T
tetrominoes[1]=[[[0,0,0,0],[1,1,1,0],[0,1,0,0],[0,0,0,0]],
[[0,1,0,0],[1,1,0,0],[0,1,0,0],[0,0,0,0]],
[[0,1,0,0],[1,1,1,0],[0,0,0,0],[0,0,0,0]],
[[0,1,0,0],[0,1,1,0],[0,1,0,0],[0,0,0,0]]];
colors[1]=0xAA00FF; //Purple
// L
tetrominoes[2]=[[[0,0,0,0],[1,1,1,0],[1,0,0,0],[0,0,0,0]],
[[1,1,0,0],[0,1,0,0],[0,1,0,0],[0,0,0,0]],
[[0,0,1,0],[1,1,1,0],[0,0,0,0],[0,0,0,0]],
[[0,1,0,0],[0,1,0,0],[0,1,1,0],[0,0,0,0]]];
colors[2]=0xFFA500; //Orange
// J
tetrominoes[3]=[[[1,0,0,0],[1,1,1,0],[0,0,0,0],[0,0,0,0]],
[[0,1,1,0],[0,1,0,0],[0,1,0,0],[0,0,0,0]],
[[0,0,0,0],[1,1,1,0],[0,0,1,0],[0,0,0,0]],
[[0,1,0,0],[0,1,0,0],[1,1,0,0],[0,0,0,0]]];
colors[3]=0x0000FF; //Blue
// Z
tetrominoes[4]=[[[0,0,0,0],[1,1,0,0],[0,1,1,0],[0,0,0,0]],
[[0,0,1,0],[0,1,1,0],[0,1,0,0],[0,0,0,0]]];
colors[4]=0xFF0000; //Red
// S
tetrominoes[5]=[[[0,0,0,0],[0,1,1,0],[1,1,0,0],[0,0,0,0]],
[[0,1,0,0],[0,1,1,0],[0,0,1,0],[0,0,0,0]]];
colors[5]=0x00FF00; //Green
// O
tetrominoes[6]=[[[0,1,1,0],[0,1,1,0],[0,0,0,0],[0,0,0,0]]];
colors[6]=0xFFFF00; //Yellow
//X
tetrominoes[7] = [[[0,1,0,0],[1,1,1,0],[0,1,0,0],[0,0,0,0]]];
colors[7] = 0x00FF80;
//U
tetrominoes[8] = [[[1,1,1,0],[1,0,1,0],[0,0,0,0],[0,0,0,0]],
[[1,1,0,0],[1,0,0,0],[1,1,0,0],[0,0,0,0]],
[[1,0,1,0],[1,1,1,0],[0,0,0,0],[0,0,0,0]],
[[0,0,1,1],[0,0,0,1],[0,0,1,1],[0,0,0,0]]];
colors[8] = 0xFF007F;
//V
tetrominoes[9] = [[[1,1,0,0],[1,0,0,0],[0,0,0,0],[0,0,0,0]],
[[0,0,1,1],[0,0,0,1],[0,0,0,0],[0,0,0,0]],
[[0,0,0,0],[0,0,0,0],[0,0,0,1],[0,0,1,1]],
[[0,0,0,0],[0,0,0,0],[1,0,0,0],[1,1,0,0]]];
colors[9] = 0xFF00FF;
//Colon
tetrominoes[10] = [[[1,1,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
[[1,0,0,0],[1,0,0,0],[0,0,0,0],[0,0,0,0]]];
colors[10] = 0x0080FF;
//Dot
tetrominoes[11] = [[[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]];
colors[11] = 0xFF4000;
}
源代码几乎相同,但BuildTetrominoes()函数中的形状7-11除外。