Game Development Reference
In-Depth Information
Step 8. Save and play to see the spaces created between the cubes.
Step 9. To create another set of cubes horizontally in the Game, add
another for loop as shown in Listing 1.22 . Note the use of the new
variable j as the x position.
Listing 1.22 A Script That Creates One Column
and One Row of Cubes
function Start ()
{
var aP : GameObject;
for(var i:int = 1; i < = 9; i++)
{
aP = GameObject.
CreatePrimitive(PrimitiveType.Cube);
aP.transform.position.y = i * 2;
}
for(var j:int = 1; j < = 9; j++)
{
aP = GameObject.
CreatePrimitive(PrimitiveType.Cube);
aP.transform.position.x = j * 2;
}
}
Step 10. Save and play. The result will look like that in Figure 1.29 . You
may need to move the camera around to see all the cubes.
Step 11. A few readjustments to this code can give you the power to
create a matrix of 9 × 9 cubes. By placing one for loop inside the other,
the nine repetitions of the horizontally placed cubes are compounded
by the nine repetitions of the vertical cubes. Modify your code to that
in Listing 1.23 . On the first pass of the outer loop, the inner loop runs
nine times. Then the outer loop moves onto its second pass. At this
time the inner loop runs nine times again. This continues until the
outer loop has finished its nine passes.
Listing 1.23 A Matrix of Cubes Created Entirely with Script
function Start ()
{
var aP : GameObject;
var numRows: int = 9;
var numCols: int = 9;
Search Nedrilad ::




Custom Search