home page icon Flash Bestiary / BASICs / Random Checkers

Random Checkers

This adds a few more features to the last movie, and enables you to interact with it a bit. The symmetry, and some other drawing effects can be turned on and off by pressing the buttons.

Note: Many of the movies in my collection have buttons or sliders which allow you to control certain features. For any sufficiently complex animation, there may be many features that may be controlled in this manner, but I will usually choose just a few for user-control, in order to keep the interface relatively simple. The code for the buttons and sliders is at the bottom of the script, and can be removed without affecting the rest of the movie.

Up till now, I've computed a random color using the following line:

var tint = Math.floor(Math.random()*0x1000000);

But now, we use the Math.sin() function to cycle thru the values for each color component (r,g and b). The variable saturation, which ranges from 0 to 1, is used to push each color component up to 120 degrees out of phase, which gives us highly saturated colors. When saturation is zero, each color component is the same and we get shades of gray.

var t = getTimer()*.001;
var r = 128+Math.sin(t)*127;
var g = 128+Math.sin(t + saturation*kDegrees120)*127;
var b = 128+Math.sin(t + saturation*kDegrees240)*127;
var tint = (r << 16) | (g << 8) | b;

Also, I've added add the following code to produce a checkerboard pattern.

if ((x + y) & 1) // Checkboard test
   tint |= 0x808080; // lighten pixel
   tint &= 0x7F7F7F; // darken pixel

For an in-depth discussion about my color cycling tricks, see my Javascript tutorial Make Annoying Rainbows.

UPDATE: The source code below now contains an Actionscript 3 version, in addition to the original Flash MX code.


sourcecode iconDownload the flash project
book iconJim's Favorite Actionscript Books
wiki iconAsk Jim about Actionscript
Next: Pseudo Mondrian

Having trouble opening the project? You may need to Upgrade to Flash CS3

Copyright © 2003,2008 by Jim Bumgardner. All Rights Reserved.        Leave Jim some feedback