I am having some trouble drawing a grid. I have 4 input text fields, the height and width control the size of the square in the grid and rows and cols control the number of rows and columns. If I change the number of rows to 10.5 and the cols to 10.5 my code doesn’t completely draw the grid. It extends the lines but doesn’t close the grid. Hope that makes sense. As long as I use a whole number 10, 11, 15 etc… the grid is completely drawn.
How can I correct this?
//
var floor = createEmptyMovieClip("Floor_mc", 10);
with (floor) {
// set x and y location of Floor_mc
_x = 0;
_y = 0;
}
//
createTextField("Width_txt", 20, -100, 0, 80, 20);
createTextField("Height_txt", 30, -100, -25, 80, 20);
//createTextField("Rows_txt", 40, 10, 0, 80, 20);
//createTextField("Cols_txt", 50, 10, 25, 80, 20);
var textFields = [Width_txt, Height_txt, Rows_txt, Cols_txt];
for (var q in textFields) {
var tf = textFields[q];
with (tf) {
border = true;
type = "input";
}
tf.onChanged = drawGrid;
}
Width_txt.text = "24";
Height_txt.text = "24";
Rows_txt.text = "10";
Cols_txt.text = "10";
//
//
function drawGrid() {
var x = floor._x;
var y = floor._y;
//
var cellWi:Number = new Number(Width_txt.text);
var cellHi:Number = new Number(Height_txt.text);
//
var rows:Number = new Number(Rows_txt.text);
var cols:Number = new Number(Cols_txt.text);
//
if (isNaN(rows)) {
rows = 1;
}
if (isNaN(cols)) {
cols = 1;
}
//
var wide = cellWi*cols;
var high = cellHi*rows;
//
clear();
lineStyle(0);
//
for (var i = 0; i<=rows; i++) {
var yy = y+i*cellHi;
//trace("yy "+yy)
moveTo(x, yy);
lineTo(x+wide, yy);
}
for (var j = 0; j<=cols; j++) {
var xx = x+j*cellWi;
//trace("xx "+xx)
moveTo(xx, y);
lineTo(xx, y+high);
}
}
//
//
drawGrid();
//
//