View comments | RSS feed

Example: RuntimeAssetsExplorer

The Export for ActionScript functionality can be especially advantageous for libraries that may be useful across more than one project. Symbols that have been exported to ActionScript are available not only to that SWF file alone but to any SWF file within the same security sandbox that loads it. In this way, a single Flash document can generate a SWF file that is designated for the sole purpose of holding graphical assets. This technique is particularly useful for larger projects where designers working on visual assets can work in parallel with developers who create a "wrapper" SWF file that then loads the graphical assets SWF file at run time. You can use this method to maintain a series of versioned files where graphical assets are not dependent upon the progress of programming development.

The RuntimeAssetsExplorer application loads any SWF file that is a subclass of RuntimeAsset and allows you to browse the available assets of that SWF file. The example illustrates the following:

Before beginning, note that each of the SWF files must be located in the same security sandbox. For more information, see Security sandboxes.

To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The RuntimeAssetsExplorer application files can be found in the folder Samples/RuntimeAssetsExplorer. The application consists of the following files:

File

Description

RuntimeAssetsExample.mxml

or

RuntimeAssetsExample.fla

The user interface for the application for Flex (MXML) or Flash (FLA).

GeometricAssets.as

An example class that implements the RuntimeAsset interface.

GeometricAssets.fla

A FLA file linked to the GeometricAssets class (the document class of the FLA) containing symbols that are exported for ActionScript.

com/example/programmingas3/runtimeassetsexplorer/RuntimeLibrary.as

An interface that defines the required methods expected of all run-time asset SWF files that will be loaded into the explorer container.

com/example/programmingas3/runtimeassetsexplorer/AnimatingBox.as

The class of the library symbol in the shape of a rotating box.

com/example/programmingas3/runtimeassetsexplorer/AnimatingStar.as

The class of the library symbol in the shape of a rotating star.

Establishing a run-time library interface

In order for the explorer to properly interact with a SWF library, the structure of the run-time asset libraries must be formalized. We will accomplish this by creating an interface, which is similar to a class in that it's a blueprint of methods that demarcate an expected structure, but unlike a class it includes no method bodies. The interface provides a way for both the run-time library and the explorer to communicate to one another. Each SWF of run-time assets that is loaded in our browser will implement this interface. For more information about interfaces and how they can be useful, see Interfaces.

The RuntimeLibrary interface will be very simple--we merely require a function that can provide the explorer with an array of classpaths for the symbols to be exported and available in the run-time library. To this end, the interface has a single method: getAssets().

package com.example.programmingas3.runtimeassetexplorer
{
    public interface RuntimeLibrary
    {
        function getAssets():Array;
    }
}

Creating the asset library SWF file

By defining the RuntimeLibrary interface, it's possible to create multiple asset library SWF files that can be loaded into another SWF file. Making an individual SWF library of assets involves four tasks:

Creating a class to implement the RuntimeLibrary interface

Next, we'll create the GeometricAssets class that will implement the RuntimeLibrary interface. This will be the document class of the FLA. The code for this class is very similar to the RuntimeLibrary interface--the difference between them is that in the class definition the getAssets() method has a method body.

package
{
    import flash.display.Sprite;
    import com.example.programmingas3.runtimeassetexplorer.RuntimeLibrary;
    
    public class GeometricAssets extends Sprite implements RuntimeLibrary 
    {
        public function GeometricAssets() {
            
        }
        public function getAssets():Array {
            return [ "com.example.programmingas3.runtimeassetexplorer.AnimatingBox",
                     "com.example.programmingas3.runtimeassetexplorer.AnimatingStar" ];    
        }
    }
}

If we were to create a second run-time library, we could create another FLA based upon another class (for example, AnimationAssets) that provides its own getAssets() implementation.

Creating classes for each MovieClip asset

For this example, we'll merely extend the MovieClip class without adding any functionality to the custom assets. The following code for AnimatingStar is analogous to that of AnimatingBox:

package com.example.programmingas3.runtimeassetexplorer
{
    import flash.display.MovieClip;
    
    public class AnimatingStar extends MovieClip
    {
        public function AnimatingStar() {
        }
    }
}

Publishing the library

We'll now connect the MovieClip-based assets to the new class by creating a new FLA and entering GeometricAssets into the Document Class field of the Property inspector. For the purposes of this example, we'll create two very basic shapes that use a timeline tween to make one clockwise rotation over 360 frames. Both the animatingBox and animatingStar symbols are set to Export for ActionScript and have the Class field set to the respective classpaths specified in the getAssets() implementation. The default base class of flash.display.MovieClip remains, as we want to subclass the standard MovieClip methods.

After setting up your symbol's export settings, publish the FLA. You now have your first run-time library. This SWF file could be loaded into another AVM2 SWF file and the AnimatingBox and AnimatingStar symbols would be available to the new SWF file.

Loading the library into another SWF file

The last functional piece to deal with is the user interface for the asset explorer. In this example, the path to the run-time library is hard-coded as a variable named ASSETS_PATH. Alternatively, you could use the FileReference class--for example, to create an interface that browses for a particular SWF file on your hard drive.

When the run-time library is successfully loaded, Flash Player calls the runtimeAssetsLoadComplete() method:

private function runtimeAssetsLoadComplete(event:Event):void
{
    var rl:* = event.target.content;
    var assetList:Array = rl.getAssets();
    populateDropdown(assetList);
    stage.frameRate = 60;
}

In this method, the variable rl represents the loaded SWF file. The code calls the getAssets() method of the loaded SWF file, obtaining the list of assets that are available, and uses them to populate a ComboBox component with a list of available assets by calling the populateDropDown() method. That method in turn stores the full classpath of each asset. Clicking the Add button on the user interface triggers the addAsset() method:

private function addAsset():void
{
    var className:String = assetNameCbo.selectedItem.data;
    var AssetClass:Class = getDefinitionByName(className) as Class;
    var mc:MovieClip = new AssetClass();
    ...
}

which gets the classpath of whichever asset is currently selected in the ComboBox (assetNameCbo.selectedItem.data), and uses the getDefinitionByName() function (from the flash.utils package) to obtain an actual reference to the asset's class in order to create a new instance of that asset.


Flash CS3


Comments


Rodislav said on Jun 26, 2007 at 1:55 AM :
anyone can help me pls!?
i writed
______________________________________
package
{
import flash.display.Sprite;
import com.example.programmingas3.runtimeassetexplorer.RuntimeLibrary;

public class GeometricAssets extends Sprite implements RuntimeLibrary
{
public function GeometricAssets() {

}
public function getAssets():Array {
return [ "com.example.programmingas3.runtimeassetexplorer.AnimatingBox",
"com.example.programmingas3.runtimeassetexplorer.AnimatingStar" ];
}
}
}


and resultats was -> 1045: Interface RuntimeLibrary was not found.
jayjayevans said on Sep 27, 2007 at 11:23 AM :
Where specifically are:
"RuntimeAssetsExplorer application files"

Did not find in either:
Samples.zip (ZIP, 8.96MB)
ActionScript3.0_Flash_Samples.zip (ZIP, 8.10MB)
djtechwriter said on Sep 27, 2007 at 11:40 AM :
Keep going. the next download on that page contains the samples related to this book (Programming AS3):
http://livedocs.adobe.com/flash/9.0/main/samples/Programming_ActionScript3.0_samples.zip
mukraker said on Oct 2, 2007 at 2:26 PM :
Hi i d like to load an external swf.
In this swf are several mc
each swf has the same frameNames
i d like to controll these frames via the document class.
But all frameactions like stop() are ignored:

// works fine
var target:MovieClip = MovieClip(event.currentTarget);
target.gotoAndPlay("someFrame");

// stops are ignored
var target:AnimatingStar = AnimatingStar(event.currentTarget)
// gotoSomeFrame() invokes gotoAndPlay("someFrame") in the document class;
target.gotoSomeFrame();
rfkrocktk said on Jan 16, 2008 at 6:20 PM :
I'm running into errors trying to run the provided code.
Flash is tracing this:
"ReferenceError: Error #1065: Variable Star is not defined.
at global/flash.utils::getDefinitionByName()
at Main/initListener()
"

I have a file called "asset_one.swf" and "Runtime Assets Example.swf", the main file being "Runtime Assets Example.swf." Here is the document class for the main file:

package {
//import fl.data.DataProvider;
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.MovieClip;
import flash.display.Loader;
import flash.system.LoaderContext;
import flash.system.ApplicationDomain;
import flash.net.URLRequest;
import flash.events.*;
import flash.utils.*;
import flash.geom.ColorTransform;

public class Main extends MovieClip {

public function Main() {
var loader:Loader = new Loader();
var request:URLRequest = new URLRequest("assets_one.swf");

loader.contentLoaderInfo.addEventListener(Event.INIT, initListener);

loader.load(request);
}
private function initListener(e:Event):void {
var ra:* = e.target.content;
var assetList:Array = ra.getAssets();
var star:String = assetList[0];
var starClass:Class = getDefinitionByName(star) as Class;
var mc:MovieClip = new starClass();
}
}

And here is the document class for "assets_one.swf":

package {
import flash.display.Sprite;

public class Assets1 extends Sprite implements RuntimeLibrary {

public function Assets1() {

}

public function getAssets():Array {
return ["Star", "Circle"];
}
}
}

Here is the interface for the RuntimeLibrary:

package {

public interface RuntimeLibrary
{
function getAssets():Array;
}
}

Here is my Star class:

package {
import flash.display.MovieClip;

public class Star extends MovieClip {

public function Star() {
this.rotation = Math.random()*360;
this.alpha = Math.random()*1;
}
}
}

Why am I receiving this error? How do I fix my code to get a working example? I'm not using the dropdown box because I'll be accessing the classes via actionscript. Did I miss something in there somewhere?

- TK
bomby said on Mar 31, 2008 at 1:11 PM :
@ rfkrocktk

I believe your problem is that your Star class is not being compiled and therefore does not exist for getDefinitionByName() to find. You can include every package under the sun in your AS file, but unless something references a certain package at compile time, the 'import' doesn't really happen and the classes aren't compiled in. You can force it to compile by creating a variable of that type

private var forceLoad:Star;

I myself am looking for a way around this but have not yet found one. I ask you... What is the point of creating a dynamic way to call a class if you must then explicitly reference each and every possible class for it to compile them in? Not very elegant. Perhaps an additional keyword 'force'. I.e.,

force import Assets.Classes.*;

Still looking for a more elegant solution.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000217.html