Testing the Movie
You’ve created your movie clips; now, let’s take the movie for a test-drive.
10. Select Control > Test Movie to preview your movie.

Figure 3.3. Preview the movie within Flash.
If everything has gone according to plan, you should see an animated random movie similar to the one shown in Figure 3.3, where the x and y coordinates of the graphics change every tenth of a second.
Random movement is simple to achieve using the basic building blocks outlined here. With experimentation, you’ll realize that the possible applications of this technique are limitless.
Modifications
It’s easy to modify the Randomizer function to alter the presentation of the movie. Changing various properties of the object at different points within the code can have quite dramatic effects—as we’re about to see.
Flickering Opacity
Returning to the Randomizer function, let’s add an extra line that alters the opacity of the graphic.
11. Locate the Randomizer function in the first frame of the MCWorld movie clip, and adjust it as follows:
function ()
{
var xShift = random (5) - 2;
var yShift = random (5) - 2;
World._x += xShift;
World._y += yShift;
World._alpha = random(100);
} 12. Save and preview the movie.
Every tenth of a second, at the same time each graphic is given a little nudge, the opacity is now reset to a random value between zero and 99%, producing a flickering effect. How easy was that?
You can even insert additional object properties to, for example, alter the horizontal and vertical scale of the object. Adding the following lines to the above code will randomly scale the graphic objects:
World._xscale = random(100);
World._yscale = random(100); Increasing the Redraw Rate
As I mentioned earlier, using setInterval to trigger the changes to our graphics disconnected this animation from the frame rate of the movie. To increase the rate of the animation, simply change the delay specified when calling setInterval:
setInterval(Randomizer, 100); Reducing this value will increase the redraw rate (how often the Randomizer function is called). Bear in mind that these values are counted in milliseconds. To change the redraw rate to one second, you’d set the value to 1000. Keep in mind that decreasing the amount of time between redraws will increase the load on the CPU. If your movie uses a large number of objects, or objects that are complex, the user’s computer might have a tough time keeping up with the changes.
Increasing the Number of Objects
To increase the number of objects initially drawn on the screen, simply change the numObjects variable in the main timeline code:
var numObjects = 30;
for (i = 0; i < numObjects; i++)
{ The for loop uses this variable to control the number of objects created, so changing the number of objects couldn’t be easier! If those objects are complex, the CPU load will also increase proportionally. Be careful!
Altering the Random Shift Value
After the objects are originally placed on the canvas at random, the Randomizer function shifts the x and y coordinates of the graphics every tenth of a second to give an appearance of jittery nervousness. To increase or decrease this quality, simply locate and edit the following lines within the Randomizer function:
var xShift = random(5) - 2;
var yShift = random(5) - 2; To keep your graphics from wandering off the stage, make sure that the first number on each line is twice the second number plus one. This relationship ensures that the calculated shift values tend to average out to zero. Of course, the easiest way to maintain this relationship is to make it explicit in the code:
var nervousness = 2;
var xShift = random(nervousness * 2 + 1) - nervousness;
var yShift = random(nervousness * 2 + 1) - nervousness; As you can see, once you become comfortable with editing the properties of objects, and randomizing their values, you can create some very interesting effects.
The key to finding out what you can do is to experiment with values, explore the ActionScript reference, and have fun!
| Download
4 sample chapters Click
here to order the 455-page book now (delivery worldwide)! |