Game Development Reference
In-Depth Information
Note
Although the sphere is being changed to a cube, note that the variable
called sphere , which is being assigned the Game Object, does not need to
be changed. This is because the name of a variable as far as the compiler
is concerned is not important. It is only named sphere in this case for
readability. It could have easily been called aPrimitive or aP .
Step 5. Imagine that you now need to add another 50 cubes on top.
This would be a big cut and paste and editing job as the y position
would need to be incremented for each new cube. Instead we will
replace all the code with just three lines (and a couple of parentheses)
that will allow you to make the stack any height you like. Modify
stackedSpheres.js to the code shown in Listing 1.21 .
Listing 1.21 Creating a Stack of Cubes with a for Loop
function Start ()
{
var aP : GameObject;
for(var i:int = 1; i < = 9; i++)
{
aP = GameObject. CreatePrimitive(PrimitiveType.
Cube);
aP.transform.position.y = i;
}
}
Note
Although the variable aP in Listing 1.21 does not have the keyword
private included, it will not become exposed. Variables declared inside
functions are called local variables and are only visible inside the function
that created them.
Step 6. Save and play the application. It will produce the same result
as the previous version. Note how the value of the variable i is being
used to set the y position of each cube? Just another advantage
of using a for loop.
Step 7. To put even spaces between each cube, change the line
aP.transform.position.y = i;
to
aP.transform.position.y = i * 2;
Search Nedrilad ::




Custom Search