13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with bitmaps<br />

The following example creates a 150 x 150 pixel BitmapData object that calls the perlinNoise() method to g<strong>en</strong>erate<br />

a gre<strong>en</strong> and blue cloud effect:<br />

package<br />

{<br />

import flash.display.Sprite;<br />

import flash.display.Bitmap;<br />

import flash.display.BitmapData;<br />

import flash.display.BitmapDataChannel;<br />

}<br />

public class BitmapNoise2 ext<strong>en</strong>ds Sprite<br />

{<br />

public function BitmapNoise2()<br />

{<br />

var myBitmapDataObject:BitmapData =<br />

new BitmapData(150, 150, false, 0x00FF0000);<br />

}<br />

}<br />

var seed:Number = Math.floor(Math.random() * 100);<br />

var channels:uint = BitmapDataChannel.GREEN | BitmapDataChannel.BLUE<br />

myBitmapDataObject.perlinNoise(100, 80, 6, seed, false, true, channels, false, null);<br />

var myBitmap:Bitmap = new Bitmap(myBitmapDataObject);<br />

addChild(myBitmap);<br />

Scrolling bitmaps<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Imagine you have created a street mapping application where each time the user moves the map you are required to<br />

update the view (ev<strong>en</strong> if the map has be<strong>en</strong> moved by just a few pixels).<br />

One way to create this functionality would be to re-r<strong>en</strong>der a new image containing the updated map view each time<br />

the user moves the map. Alternatively, you could create a large single image and use the scroll() method.<br />

The scroll() method copies an on-scre<strong>en</strong> bitmap and th<strong>en</strong> pastes it to a new offset location—specified by (x, y)<br />

parameters. If a portion of the bitmap happ<strong>en</strong>s to reside off-stage, this gives the effect that the image has shifted. Wh<strong>en</strong><br />

combined with a timer function (or an <strong>en</strong>terFrame ev<strong>en</strong>t), you can make the image appear to be animating or<br />

scrolling.<br />

The following example takes the previous perlin noise example and g<strong>en</strong>erates a larger bitmap image (three-fourths of<br />

which is r<strong>en</strong>dered off-stage). The scroll() method is th<strong>en</strong> applied, along with an <strong>en</strong>terFrame ev<strong>en</strong>t list<strong>en</strong>er that<br />

offsets the image by one pixel in a diagonally downward direction. This method is called each time the frame is <strong>en</strong>tered<br />

and as a result, the off scre<strong>en</strong> portions of the image are r<strong>en</strong>dered to the Stage as the image scrolls down.<br />

Last updated 6/6/2012<br />

252

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!