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 movie clips<br />

package com.example.programmingas3.runtimeassetexplorer<br />

{<br />

import flash.display.MovieClip;<br />

}<br />

public class AnimatingStar ext<strong>en</strong>ds MovieClip<br />

{<br />

public function AnimatingStar() {<br />

}<br />

}<br />

Publishing the library<br />

We’ll now connect the MovieClip-based assets to the new class by creating a new FLA and <strong>en</strong>tering GeometricAssets<br />

into the Docum<strong>en</strong>t Class field of the Property inspector. For the purposes of this example, we’ll create two very basic<br />

shapes that use a timeline twe<strong>en</strong> to make one clockwise rotation over 360 frames. Both the animatingBox and<br />

animatingStar symbols are set to Export for ActionScript and have the Class field set to the respective classpaths<br />

specified in the getAssets() implem<strong>en</strong>tation. The default base class of flash.display.MovieClip remains, as we<br />

want to subclass the standard MovieClip methods.<br />

After setting up your symbol’s export settings, publish the FLA. You now have your first run-time library. This SWF<br />

file could be loaded into another AVM2 SWF file and the AnimatingBox and AnimatingStar symbols would be<br />

available to the new SWF file.<br />

Loading the library into another SWF file<br />

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

The last functional piece to deal with is the user interface for the asset explorer. In this example, the path to the runtime<br />

library is hard-coded as a variable named ASSETS_PATH. Alternatively, you could use the FileRefer<strong>en</strong>ce class—for<br />

example, to create an interface that browses for a particular SWF file on your hard drive.<br />

Wh<strong>en</strong> the run-time library is successfully loaded, Flash Player calls the runtimeAssetsLoadComplete() method:<br />

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

{<br />

var rl:* = ev<strong>en</strong>t.target.cont<strong>en</strong>t;<br />

var assetList:Array = rl.getAssets();<br />

populateDropdown(assetList);<br />

stage.frameRate = 60;<br />

}<br />

In this method, the variable rl repres<strong>en</strong>ts the loaded SWF file. The code calls the getAssets() method of the loaded<br />

SWF file, obtaining the list of assets that are available, and uses them to populate a ComboBox compon<strong>en</strong>t with a list<br />

of available assets by calling the populateDropDown() method. That method in turn stores the full classpath of each<br />

asset. Clicking the Add button on the user interface triggers the addAsset() method:<br />

Last updated 6/6/2012<br />

333

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

Saved successfully!

Ooh no, something went wrong!