Reputation: 37
I have a two dimensional array and I want to know how I can refer to the rows and columns in it. Do I use [row,column] or [column,row]? I also have some graphics. Do I calculate the (x,y) coordinate set of each graphic with (row*size,col*size) or with (col*size,row*size)?
The whole two dimensional array is the building instructions for the grid. Each element in the array refers to a piece of the grid. I know how to construct this grid and its pieces and I know how to access and manipulate the array's elements.
The problem is that when I construct the grid I have to calculate the x and y coordinate of each piece, but I just don't know if my variable curRow should be used for the x or y coordinate. It's similarly with the variable curCol.
My code is working, but it confuses me. I think of it like the rows control the y coordinates and the columns control the x coordinates, because I just learned of the way matrices are referred to. I ask, because it came to my mind that I am unsure of how to do this. In the past I have used [row,column] to loop and (row*size,col*size) to position.
The code so far is:
function buildGrid(gridInfo:Array):Sprite {
var displaySprite:Sprite = new Sprite();
for(var curRow:uint=0;curRow<gridInfo.length;curRow++) {
for(var curCol:uint=0;curCol<gridInfo[curRow].length;curCol++) {
var infoRef:Object = gridInfo[curRow][curCol];//create reference for fast access
var pieceGraphic:Shape = new Shape();
pieceGraphic.graphics.beginFill(infoRef.fillColor);
pieceGraphic.graphics.lineStyle(infoRef.borderThickness,infoRef.theBorderColor);
pieceGraphic.graphics.drawRect(0,0,infoRef.sideLength,infoRef.sideLength);
pieceGraphic.graphics.endFill();
pieceGraphic.x = curRow*(infoRef.sideLength+infoRef.spaceX);//later use of graphic requires known x
pieceGraphic.y = curCol*(infoRef.sideLength+infoRef.spaceY);//later use of graphic requires known y
displaySprite.addChild(pieceGraphic);
}
}
return displaySprite;
}
Upvotes: 1
Views: 1240
Reputation: 37
It's correct to refer to a two dimensional array with [row,column], but it's easier with other variable names like curX and curY. It's important to be consistent of what the indexes mean throughout the project and what indexes are used. To access the first elements in the array create a loop for rows or for curX and for the elements in the second dimension create a loop for columns or for curY.
You can calculate coordinate sets (x,y) for graphics in a grid with (row*size,col*size) or (curX*size,curY*size), so your code is correct and doesn't need to be changed.
Remember that rows are horizontal and columns are vertical.
Upvotes: 0