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 />

Working with bitmaps<br />

Loading an external image is similar to loading an external SWF; both use an instance of the flash.display.Loader class<br />

to perform the loading operation. The actual code in the MoonSphere() method that starts loading the image is as<br />

follows:<br />

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

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

imageLoader.load(new URLRequest("moonMap.png"));<br />

The first line declares the Loader instance named imageLoader. The third line actually starts the loading process by<br />

calling the Loader object’s load() method, passing a URLRequest instance repres<strong>en</strong>ting the URL of the image to load.<br />

The second line sets up the ev<strong>en</strong>t list<strong>en</strong>er that will be triggered wh<strong>en</strong> the image has completely loaded. Notice that the<br />

addEv<strong>en</strong>tList<strong>en</strong>er() method is not called on the Loader instance itself; instead, it’s called on the Loader object’s<br />

cont<strong>en</strong>tLoaderInfo property. The Loader instance itself doesn’t dispatch ev<strong>en</strong>ts relating to the cont<strong>en</strong>t being loaded.<br />

Its cont<strong>en</strong>tLoaderInfo property, however, contains a refer<strong>en</strong>ce to the LoaderInfo object that’s associated with the<br />

cont<strong>en</strong>t being loaded into the Loader object (the external image in this case). That LoaderInfo object does provide<br />

several ev<strong>en</strong>ts relating to the progress and completion of loading the external cont<strong>en</strong>t, including the complete ev<strong>en</strong>t<br />

(Ev<strong>en</strong>t.COMPLETE) that will trigger a call to the imageLoadComplete() method wh<strong>en</strong> the image has completely<br />

loaded.<br />

While starting the external image loading is an important part of the process, it’s equally important to know what to<br />

do wh<strong>en</strong> it finishes loading. As shown in the code above, the imageLoadComplete() function is called wh<strong>en</strong> the image<br />

is loaded. That function does several things with the loaded image data, described subsequ<strong>en</strong>tly. However, to use the<br />

image data, it needs to access that data. Wh<strong>en</strong> a Loader object is used to load an external image, the loaded image<br />

becomes a Bitmap instance, which is attached as a child display object of the Loader object. In this case, the Loader<br />

instance is available to the ev<strong>en</strong>t list<strong>en</strong>er method as part of the ev<strong>en</strong>t object that’s passed to the method as a parameter.<br />

The first lines of the imageLoadComplete() method are as follows:<br />

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

{<br />

textureMap = ev<strong>en</strong>t.target.cont<strong>en</strong>t.bitmapData;<br />

...<br />

}<br />

Notice that the ev<strong>en</strong>t object parameter is named ev<strong>en</strong>t, and it’s an instance of the Ev<strong>en</strong>t class. Every instance of the<br />

Ev<strong>en</strong>t class has a target property, which refers to the object triggering the ev<strong>en</strong>t (in this case, the LoaderInfo instance<br />

on which the addEv<strong>en</strong>tList<strong>en</strong>er() method was called, as described previously). The LoaderInfo object, in turn, has<br />

a cont<strong>en</strong>t property that (once the loading process is complete) contains the Bitmap instance with the loaded bitmap<br />

image. If you want to display the image directly on the scre<strong>en</strong>, you can attach this Bitmap instance<br />

(ev<strong>en</strong>t.target.cont<strong>en</strong>t) to a display object container. (You could also attach the Loader object to a display object<br />

container). However, in this sample, the loaded cont<strong>en</strong>t is used as a source of raw image data rather than being<br />

displayed on the scre<strong>en</strong>. Consequ<strong>en</strong>tly, the first line of the imageLoadComplete() method reads the bitmapData<br />

property of the loaded Bitmap instance (ev<strong>en</strong>t.target.cont<strong>en</strong>t.bitmapData) and stores it in the instance variable<br />

named textureMap, which is used as a source of the image data to create the animation of the rotating moon. This is<br />

described next.<br />

Last updated 6/6/2012<br />

255

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

Saved successfully!

Ooh no, something went wrong!