Random Pattern Background
| Category | : Flash | Views | : 30482 | ||
| Version | : 8 | Rating | : | ||
| Type | : Text | Source File | : random-pattern-background.zip | ||
| Result | : See the result | ||||
The result will be different.

Code Explanation
import flash.display.BitmapData
This line of code imports the BitmapData library to our code so we can create a BitmapData object to hold our image
var patNr = 4;
The number of patterns we have available to load.
nPattern.onRelease = function()
{
Event for when the user clicks the pattern button.
var img = "pat" + Math.ceil(Math.random() * patNr);
Variable to use as identifier when we load the image from the library; the variable will be "pat" plus a random number between 1 and patNr.Because "Math.random() * patNr" would return a number between 0 and patNr and I don't have a pattern with the identifier "pat0" I need to round up the random number so I never get 0. For that we use the following function:
Math.ceil(number);
This function will always round a floating point up.
_root.createEmptyMovieClip("holder", 1);
Create a new Movie Clip to place a pattern.
var imgBitmap = BitmapData.loadBitmap(img);
This variable is a Bitmap object created with the function loadBitmap(identifier), this function will load a bitmap from the library using the identifier.
holder.clear();
Erase any previous pattern on the Movie Clip.
holder.beginBitmapFill(imgBitmap);
Start filling the Movie Clip with the pattern bitmap.
holder.lineTo(550, 0);
holder.lineTo(550, 400);
holder.lineTo(0, 400);
holder.lineTo(0, 0);
Draw around the stage to make the pattern fill everything.
holder.endFill();
Stop filling the bitmap.
nPattern.swapDepths(33);
Swap the depth of the button so it always stays on top of the pattern and the user can see it.
![]() |
![]() |
![]() |
![]() |
![]() |







