Graphics Programs Reference
In-Depth Information
Step 2: Replenish the rocks
Since we appear to be in rather deep space, we might expect to run into more rocks.
We could increase the number of rocks that we duplicate, but that might bog things
down at some point. Instead let's use our check for an object being too close to the
viewer as an opportunity to recycle rocks. Instead of using removeMovieClip() to
delete a rock, let's just send it back into deep space by using a call to the placeObj()
function as shown in line 55. Again test your movie. You should now have several light
years of rocks to go through.
52
53
54
55
56
// check if the object is too close to the viewer
if ( thisObj.z <= -d + 30)
{
placeObj();
}
Step 3: Add movement in the x- and y-directions
We're making progress, but it doesn't seem reasonable that all of the rocks would be
moving only along the z-axis. Let's provide some movement in the x- and y-directions
for the rocks as well. We can randomly vary these movements in a manner similar to
what we did in the z-direction. The effect won't be as striking as when we varied the
change in the z-direction, but it provides a wider variety of crossing directions. Replace
line 27 with the lines shown below to generate the incremental movement of each rock.
27
28
29
30
31
32
33
//move random incremental distance in x-,y-,z-directions
thisObj.dx = Math.random() * 10 - 5;
thisObj.dy = Math.random() * 10 - 5;
thisObj.dz = -Math.random() * 10 - 3;
}
We will need to add the incremental movement to the current position of each rock to
get an updated position. We are already updating the z - location of each rock and just
need to add similar expressions for x and y as shown. Note that we could have defined
a separate update function to include these lines and then call the update function.
Alternatively, we could also have placed the lines in the displayObj() function. Since
we already had the z - value update, it seemed easier to just add the additional lines at
this location in the script.
 
Search WWH ::




Custom Search