O'Reilly logo

ActionScript 3.0 Cookbook by Joey Lott, Darron Schall, Keith Peters

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Dissolving Between Two Bitmaps

Problem

You want to fade between one bitmap and another.

Solution

Use the pixelDissolve() method of the BitmapData class.

Discussion

Like many of the other BitmapData methods, pixelDissolve() uses a source and destination bitmap. Each time it is called, it copies a specified number of random pixels from the source bitmap to the destination. To complete a full dissolve, call this method repeatedly, either in an enterFrame handler or a timer-based function. See Recipe 11.1 for information on timer-based animation.

The first time the pixelDissolve() method is called, you pass it a random number to use as a seed for the first round of pixel copying. It returns a number that you should keep track of and use as the seed for the next iteration.

Here is the syntax for the method:

seed = srcBmp.pixelDissolve(destBmp, sourceRect, destPoint, 
                            seed, numPixels, fillColor);
  • The sourceRect and destPoint parameters work the same way they do in other BitmapData methods that use two bitmaps. The seed is explained more in Recipe 8.10.

  • The numPixels parameter determines how many pixels are copied each time the method is called.

  • The fillColor offers an easy way of fading a single bitmap to a specified color. If you use the same BitmapData as source and destination, instead of copying identical pixels from one to the other (which would have no visible effect), the chosen pixels are set to the fill color specified in this parameter. You can ignore this when using two different ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required