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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Filtering display objects<br />

import flash.display.BitmapData;<br />

import flash.display.Loader;<br />

import flash.ev<strong>en</strong>ts.MouseEv<strong>en</strong>t;<br />

import flash.filters.Displacem<strong>en</strong>tMapFilter;<br />

import flash.geom.Point;<br />

import flash.net.URLRequest;<br />

// Load an image onto the Stage.<br />

var loader:Loader = new Loader();<br />

var url:URLRequest = new URLRequest("http://www.helpexamples.com/flash/images/image3.jpg");<br />

loader.load(url);<br />

this.addChild(loader);<br />

var mapImage:BitmapData;<br />

var displacem<strong>en</strong>tMap:Displacem<strong>en</strong>tMapFilter;<br />

// This function is called wh<strong>en</strong> the image finishes loading.<br />

function setupStage(ev<strong>en</strong>t:Ev<strong>en</strong>t):void<br />

{<br />

// C<strong>en</strong>ter the loaded image on the Stage.<br />

loader.x = (stage.stageWidth - loader.width) / 2;<br />

loader.y = (stage.stageHeight - loader.height) / 2;<br />

}<br />

// Create the displacem<strong>en</strong>t map image.<br />

mapImage = new BitmapData(loader.width, loader.height, false, 0xFF0000);<br />

// Create the displacem<strong>en</strong>t filter.<br />

displacem<strong>en</strong>tMap = new Displacem<strong>en</strong>tMapFilter();<br />

displacem<strong>en</strong>tMap.mapBitmap = mapImage;<br />

displacem<strong>en</strong>tMap.mapPoint = new Point(0, 0);<br />

displacem<strong>en</strong>tMap.compon<strong>en</strong>tX = BitmapDataChannel.RED;<br />

displacem<strong>en</strong>tMap.scaleX = 250;<br />

loader.filters = [displacem<strong>en</strong>tMap];<br />

loader.cont<strong>en</strong>tLoaderInfo.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.COMPLETE, setupStage);<br />

The properties used to define the displacem<strong>en</strong>t are as follows:<br />

Map bitmap: The displacem<strong>en</strong>t bitmap is a new BitmapData instance created by the code. Its dim<strong>en</strong>sions match the<br />

dim<strong>en</strong>sions of the loaded image (so the displacem<strong>en</strong>t is applied to the <strong>en</strong>tire image). It is filled with solid red pixels.<br />

Map point: This value is set to the point 0, 0—again, causing the displacem<strong>en</strong>t to be applied to the <strong>en</strong>tire image.<br />

X compon<strong>en</strong>t: This value is set to the constant BitmapDataChannel.RED, meaning the red value of the map bitmap<br />

will determine how much the pixels are displaced (how much they move) along the x axis.<br />

X scale: This value is set to 250. The full amount of displacem<strong>en</strong>t (from the map image being completely red) only<br />

displaces the image by a small amount (roughly one-half of a pixel), so if this value was set to 1 the image would<br />

only shift .5 pixels horizontally. By setting it to 250, the image shifts by approximately 125 pixels.<br />

Last updated 6/6/2012<br />

287

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

Saved successfully!

Ooh no, something went wrong!