这段代码应该绘制一个网格(它是用于迷宫的,所以我只需要绘制它的一部分):
function drawMaze() {
for (var r = 0; r < Rows.length; r++) {
var row = Rows[r];
var sr = new createjs.Shape();
sr.x = 0;
sr.y = (r + 1) * offY;
var gr = sr.graphics.beginStroke("black");
gr.moveTo(0, (r + 1) * offY);
for (var i = 0; i < row.length; i++) {
if (row[i] === true) {
console.log("r=" + r + ",i=" + i + ",x=" + (i + 1) * offX + ",y=" + (r + 1) * offY);
gr.lineTo((i + 1) * offX, (r + 1) * offY);
} else {
gr.moveTo((i + 1) * offX, (r + 1) * offY);
}
}
stage.addChild(sr);
}
for (var c = 0; c < Cols.length; c++) {
var col = Cols[c];
var sc = new createjs.Shape();
sc.x = (c + 1) * offX;
sc.y = 0;
var gc = sc.graphics.beginStroke("black");
gc.moveTo((c + 1) * offX, 0);
for (var i = 0; i < col.length; i++) {
if (col[i] === true) {
console.log("c=" + c + ",i=" + i + ",x=" + (c + 1) * offX + ",y=" + (i + 1) * offY);
gc.lineTo((c + 1) * offX, (i + 1) * offY);
} else {
gc.moveTo((c + 1) * offX, (i + 1) * offY);
}
}
stage.addChild(sc);
}
var sd = new createjs.Shape();
sd.graphics.s("black").mt(0, 0).lt(offX * Cols.length, offY * Rows.length).f("red").r(175,175,175,175);
stage.addChild(sd);
stage.update();
}
对角线/红色方块仅用于可视化。这样,行/列之间的间距加倍,因此只有一半的行形成网格。这是完整的代码:http: //jsfiddle.net/vyLRT/9/